Wikipedia says: In earlier and in literary usage, the word means “carefree”, “happy”, or “bright and showy”. From the 1890s, it had begun to carry a connotation of promiscuity, as in a “gay house” referring to a brothel. It began to be used in reference to homosexuality in particular from the early 20th century, from the 1920s at the latest.
The more technical answer and a very complete one can be found here:
http://www.answerbag.com/q_view/648687
Here are a couple of excerpts:
There are two, not necessarily mutually exclusive, commonly proffered explanations that are plausible.
Perhaps the most commonly touted one is that the modern use of gay comes from a clipping of gaycat, a slang term among hobos and itinerants meaning a boy or young man who accompanies an older, more experienced tramp, with the implication of sexual favors being exchanged for protection and instruction.
The second possible explanation is that the homosexual sense is an outgrowth of an earlier sense of gay meaning addicted to pleasure, self-indulgent, or immoral. This sense dates to at least 1637, when it appears in James Shirley’s play The Lady of Pleasure:
Lord. You’le not be angry, Madam.
Cel. Nor rude, though gay men have a priviledge [sic].
By the early 19th century, this sense had developed into a euphemism for prostitution. From John Davis’s 1805 The Post-Captain:
As our heroes passed along the Strand, they were accosted by a hundred gay ladies, who asked them if they were good-natured. “Devil take me!...there is not a girl in the Strand that I would touch with my gloves on.”
This could easily have transferred to male prostitutes and then generalized to mean homosexual writ large.