From the: Maryland Historical Society

Francis Scot Key, Col. Skinner, and Dr. Beanes watched the shelling on Fort McHenry during the night of September 13, 1814.
The British land assault on Baltimore as well as the naval attack, had been abandoned and the British ordered a retreat.

Waiting in the predawn darkness, Key waited for the sight that would end his anxiety; the joyous sight of Gen. Armistead's great flag blowing in the breeze at the Fort. When at last daylight came, The Flag Was Still There!

Francis began to write a poem on the back of a letter he had about he just witnessed. Finishing it later in the day back in Baltimore on September 14th, it was published by his brother-in-law, Judge J. H. Nicholson, titled "Defence of Fort McHenry".

September 20, 1814, newspapers printed the poem. To the verses was added a note "Tune: Anacreon in Heaven."

In October 1814, a Baltimore actor, Ferdinard Durang, sang Key's new song in a public performance and
 called it "The Star-Spangled Banner". Source