No1 really knows where and when gay got the meaning of homosexual really, but it's sure that it was used in 1920, and generally adopted by the public in 1960, but it could have been known and used earlier.
The most likely explanation is that it derives from gaycat or geycat, a slang term for a tramp or hobo who is new to the road. Gaycats were commonly in the company of older tramps, implying a homosexual relationship. The term, according to Lighter, dates to at least the 1890s. Gaycats were employed as lookouts while other hoboes committed crimes. The OED2 cites the 1935 Underworld & Prison Slang by N. Ersine as defining geycat as a homosexual boy. The origin of gaycat is unknown. Green, however, says a gay cat was a tramp who offered sexual services to women.
Another possible origin is the late nineteenth century slang usage of gay to mean promiscuous. A gay house meant a brothel. This sexual sense of the term could have become associated with homosexual promiscuity and the heterosexual sense lost.