Because Key and Skinner had heard details of the plans for the attack on Baltimore, they were held captive until after the battle, first aboard HMS Surprise and later back on HMS Minden.
Key was inspired by the American victory and the sight of the large American flag flying triumphantly above the fort. Aboard the ship the next day, Key wrote a poem on the back of a letter he had kept in his pocket. At twilight on September 16, he and Skinner were released in Baltimore. He completed the poem at the Indian Queen Hotel, where he was staying, and titled it "Defence of Fort M'Henry".
Much of the idea of the poem, including the flag imagery and some of the wording, is derived from an earlier song by Key, also set to the tune of "The Anacreontic Song". The song, known as "When the Warrior Returns", was written in honor of Stephen Decatur and Charles Stewart on their return from the First Barbary War. Absent elaboration by Francis Scott Key prior to his death in 1843, some have speculated in modern times about the meaning of phrases or verses.
According to British historian Robin Blackburn, the words "the hireling and slave" allude to the thousands of ex-slaves in the British ranks organised as the Corps of Colonial Marines, who had been liberated by the British and demanded to be placed in the battle line "where they might expect to meet their former masters."
Nevertheless, Professor Mark Clague, a professor of musicology at the University of Michigan, argues that the "middle two verses of Key's lyric vilify the British enemy in the War of 1812" and "in no way glorifies or celebrates slavery." Clague writes that "For Key ... the British mercenaries were scoundrels and the Colonial Marines were traitors who threatened to spark a national insurrection."
This harshly anti-British nature of Verse 3 led to its omission in sheet music in World War I, when Britain and the U.S. were allies. Responding to the assertion of writer Jon Schwarz of The Intercept that the song is a "celebration of slavery," Clague said that: "The reference to slaves is about the use, and in some sense the manipulation, of black Americans to fight for the British, with the promise of freedom. The American forces included African-Americans as well as whites. The term 'freemen,' whose heroism is celebrated in the fourth stanza, would have encompassed both."
Others suggest that "Key may have intended the phrase as a reference to the British Navy’s practice of impressment (kidnapping sailors and forcing them to fight in defense of the crown), or as a semi-metaphorical slap at the British invading force as a whole (which included a large number of mercenaries)."
https://en.wikipedia.org/wiki/The_Star-Spangled_Banner