The tune was written, circa 1855, by South Carolinian William Steffe. The lyrics at that time were alternately called "Canaan's Happy Shore" or "Brothers, Will You Meet Me?" and the song was sung as a campfire spiritual. The tune spread across the United States, taking on many sets of new lyrics.
A man from Vermont named Thomas Bishop joined the Massachusetts Infantry before the outbreak of war and wrote a popular set of lyrics, circa 1860, titled "John Brown's Body" which became one of his unit's walking songs. According to writer Irwin Silber (who has written a book about Civil War folksongs), the song was not about John Brown, the famed abolitionist, but a Scotsman of the same name who was a member of the 12th Massachusetts Regiment. An article by writer Mark Steyn explains that the men of John Brown's unit had made up a song poking fun at him, and sang it widely.
Bishop's battalion was dispatched to Washington, D.C. early in the Civil War, and Julia Ward Howe heard this song during a public review of the troops in Washington. As with many others, she assumed it was about John Brown the abolitionist. Her companion at the review, the Reverend James Clarke, suggested to Howe that she write new words for the fighting men's song, and the current version of "Battle Hymn of the Republic" was born [1].
Howe's "The Battle Hymn of the Republic" was first published on the front page of The Atlantic Monthly of February 1862. The sixth verse written by Howe, which is less commonly sung, was not published at that time.