Where did this song come from?
... The words "hail to the chief" first referred not to a president but to a Scottish chieftain. They come from a romantic poem by Sir Walter Scott called "The Lady of the Lake," first published in 1810. The poem was so popular it was quickly adapted into a London musical, which before long migrated across the Atlantic to the newly independent United States.
The song, possibly adapted from an old Scottish air, was written for the musical by English composer James Sanderson.
How did it get introduced to use with the President?
In America, it was quickly fitted with new lyrics and a new name—"Wreaths for the Chieftain"—and was first used to honor a U.S. president at an 1815 birthday celebration for the late George Washington. The first time it was used for a living president came when the Marine Band performed it for John Quincy Adams at an 1828 groundbreaking ceremony for the Chesapeake and Ohio Canal.
This doesn't meant it was always used:
The song hasn't been to every chief executive's liking. President Chester Arthur (1881-1885), didn't find it dignified and so asked U.S. Marine Band Director John Philip Sousa to write something else. Sousa came up with the "Presidential Polonaise," but according to the Marine Band's Web site, "it never completely replaced 'Hail to the Chief' and was soon abandoned."
In 1954, the Department Defense made "Hail to the Chief" the offiical music for presidential events.