Because even early 1900's songs and plays used gay commonly to mean happy; content.
Even in the early 1900's, the word "gay" didn't just mean happy.
Even in its earlier meanings, going back to the 12th centry, it meant joyful, carefree, uninhibited, bright and showy, etc.
By the early 1800's, the word's implication of uninhibited pleasure had become associated with immorality and lack of sexual inhibitions. Oxford dictionary at the time changed its definition of the word to include "addicted to pleasures and dissipations. Often euphemistically: Of loose and immoral life”.
The word became associated with illicit sex and prostitution. A promiscuous man might be euphemistically called a "gay lothario"; a prostitute might be called a "gay woman," a brothel called a "gay house."
By the 1920's the euphemistic use of the word had started to shift from implying sexual promiscuity in general, to specifically gay sex when the word was applied to men. The ongoing association with "bright and showy" clothing also became associated with frivolity and femininity in men.
But at the same time, the word also continued to be used casually to just mean carefree or joyous. To call a man "gay" to imply he was homosexual was a bit like saying he was "artistic" in the same manner, or to say a woman was "sporty" to imply she was lesbian. There was no non-degrading term for homosexual at the time.
In the mid-50's, gay people had started to organize and develop a sense of group identity. Within these groups, there was a desire for a new word to identify themselves with that was neither a slur ("sodomite" and etc), nor pathologizing ("homosexual", which was a psychiatric diagnosis of mental illness). For decades the word "gay" had been used among homosexual men as a non-insulting euphemism, so in the mid-50's this word was adopted as a direct term of identity.
After Stonewall and the start of the modern gay rights movement in 1969, the use of the word "gay" to directly mean "homosexual" became public knowledge. Which pretty much ended the casual use of the word to mean "carefree."