Ultimately, because somebody replaced the words of one tune with other words. A "new" song that simply replaces the words of an old one is called a "contrafactum," and it's been a practice in the west since at least the 13th century. I would be surprised if it weren't an extremely widespread practice (it seems like it would shade into other sorts of quasi-formulaic improvised song, like epic poetry or jeliya), but I can't speak about other musical traditions with much authority.
On "God Save the Queen" in particular, it seems that the song (with its pro-monarch lyrics) rose to popularity in England in the mid-1700s in response to the Jacobite Uprising in 1745. (Edward Dibdin, "The Bi-Centenary of 'Rule Britannia'" Music & Letters 21/3 (1940); 275-290.) It's understandable that such lyrics wouldn't have been popular in the US after the war, and Oxford Music Online reports that:
In the Amer. colonies and the USA the tune has at different times been sung to many different sets of words, e.g. God Save America, God Save George Washington, God Save the Thirteen States, etc. The present words My country, 'tis of Thee, date from 1831 and are the work of the Rev. Samuel Francis Smith . The name usually given to the tune is America.
The tune itself was popular enough to be worked into several classical compositions, by for example Beethoven (who wrote variations on it) and Rossini (who used it at the end of his opera Il viaggio a Reims as part of a mashup of various national anthems).
Many tunes from the 18th century survive with various texts today. One of the most well-known is surely the French song "Ah! vous dirai-je, maman" that Americans now know as "Twinkle, twinkle, little star," "Baa baa black sheep" and the alphabet song. (Also probably worth mentioning that "Happy Birthday" is a contrafactum on an original song, whose text was "Good morning to all," though this song was composed around 1900.) The German poet Goethe is documented to have done this sort of thing, retexting German folk songs as a poetic exercise and convivial activity. (Frederick Sternfeld, "The Musical Springs of Goethe's Poetry," Musical Quarterly 35/4 (1949): 511-27.)
It's striking (though perhaps not surprising) that, as a practice, this has happened more than once with national anthems and patriotic songs. Franz Joseph Haydn's anthem for Austria, originally texted "Gott erhalte Franz den Kaiser" for Francis II, has been retexted many times, most notoriously as "Deutschland, Deutschland über alles" (whose text was written by Hoffmann von Fallersleben). Other pretty well-known examples of the same practice come from the American Civil War: "John Brown's Body" being retexted as the "Battle Hymn of the Republic," and the competing Union & Confederate texts for the "Battle Cry of Freedom."