On the night of September 13, 1814, Francis Scott Key, an American lawyer and amateur poet, accompanied American Prisoner Exchange Agent Colonel John Stuart Skinner to negotiate a prisoner release with several officers of the British Navy. During the negotiations, Key and Skinner learned of the British intention to attack the city of Baltimore, as well as strength and positions of British forces. They were held captive for the duration of the battle and witnessed the bombardment of Baltimore's Fort McHenry. Inspired by the American victory and the sight of the American flag flying high in the morning, Key wrote a poem titled "The Defence of Fort McHenry."
Upon his return to Baltimore, Key gave the poem to his brother-in-law, who noted that the words fit melody to the popular drinking song, "The Anacreontic Song." Key's brother-in-law took the poem to a printer, who made broadside copies of it. A few days later, the Baltimore Patriot and The American printed the poem with the note "Tune: Anacreon in Heaven." Later, the Carr Music Store in Baltimore published the words and music together as "The Star-Spangled Banner."
The song gained popularity over the course of the nineteenth century and was often played at public events like parades and Independence Day celebrations (and, on occasion, sporting events). In 1889, the Secretary of the Navy ordered it the official tune to be played during the raising of the flag. In 1916, President Woodrow Wilson ordered that it be played at all military ceremonies and other appropriate occasions, making it something of an unofficial national anthem.
After America's entrance into World War I, Major League Baseball games often featured patriotic rituals, such as players marching in formation during pregame military drills and bands playing patriotic songs. During the seventh-inning stretch of game one of the 1918 World Series, the band erupted into "The Star-Spangled Banner." The Cubs and Red Sox players faced the centerfield flag pole and stood at attention. The crowd, already on their feet, began to sing along and applauded at the end of the song.
Given the positive reaction, the band played the song during the next two games, and when the Series moved to Boston, the Red Sox owner brought in a band and had the song played before the start of each remaining contest. After the war (and after the song was made the national anthem by a congressional resolution in 1931), the song continued to be played, but only on special occasions like opening day, national holidays and World Series games.
During World War II, baseball games again became venues for large-scale displays of patriotism, and technological advances in public address systems allowed songs to be played without a band. "The Star-Spangled Banner" was played before games throughout the course of the war, and by the time the war was over, the pregame singing of the national anthem had become cemented as a baseball ritual, after which it spread to other sports.
This question was asked semi-rhetorically by the guy sitting next to me in the waiting room of the animal hospital while we watched the NBA Finals and waited for our pets to be healed. I hope his dog, Maverick, is ok. If you've got a burning question that you'd like to see answered here, shoot me an email at flossymatt (at) gmail.com. Twitter users can also make nice with me and ask me questions there. Be sure to give me your name and location (and a link, if you want) so I can give you a little shout out.