The Oxford English Dictionary does a very good job of researching into the origins of various word usage, and usually provide the earliest known appearance of said word in print.
According to the OED, the word "gay" in the sense to mean "homosexual" dates from the early 20th century:
gay orig. U.S. slang. (a) Of a person: homosexual; (b) (of a place, milieu, way of life, etc.) of or relating to homosexuals.Although more frequently used of male homosexuals, this sense can either include or exclude lesbians: see, for example, quots. 1962 and 1993.
The 1962 and 1993 quotes that the definition alludes to are the "lesbian pulp novel" Beebo Brinker by Ann Bannon, and what I suppose is an April 1993 edition of a magazine called Spin. The OED provides many cited examples predating Bannon's novel, although they caution:
A number of quotations have been suggested as early attestations of this sense (see a sample below). It is likely that, although there may be innuendo in some cases, these have been interpreted anachronistically in the light either of the context (for example the disguise as a homosexual of the protagonist of quot. 19411), or of knowledge about an author's sexuality.
So it is with a grain of salt that we should read their earliest-cited print appearance of "gay" in this sense, in Gertrude Stein's 1922 prose poem Miss Furr and Miss Skeene in which she says, of the two women protagonists:
Helen Furr and Georgina Keene lived together then... They were together then and traveled to another place and stayed there and were gay there..not very gay there, just gay there. They were both gay there.
The first citation provided by the OED that seems to legitimately use "gay" in the sense you mean is from the October 17, 1933, issue of the Baltimore Afro-American, which, coincidentally, Wikipedia describes as "the longest-running African-American family-owned newspaper in the United States". The Afro's usage is very interesting, historically speaking:
The products engendered by union of these decadents of changing sexes is generally an unenviable type of degeneracy... Sissies, fairies, pansies gay, The woods are full of them today.
The word here is obviously adjectival, but the usage seems to be closer to its historically more popular sense of "happy, carefree" than to homosexuality. However, as an adjective, it is modifying the noun "pansies," a familiar albeit derogatory name for effeminate (usually with the connotation of homosexuality) men. The OED traces this sense of "pansy" back to at least 1926, where the May issue of Life magazine describes a "series of sketches" called All About Sex, which deals "with gentlemen hiding under beds and spectacular numbers showing the different kinds of pansies in the world's history."
Thus, The Afro's allusion to "pansies gay" would seem to refer to happily effeminate men, which is supported by the usage of "sissies" and "fairies" in the same line. Perhaps we are witnessing in this usage the transitory period where "gay" moved from meaning "happy" or "lewd, lascivious" to meaning "homosexual."
Finally, the OED offers some thoughts on the origin of this usage of "gay," which I will quote at length here. Forgive me for the long excerpt, but I believe it to be pertinent.
The relationship between the various subsenses of sense A. 4 [Wanton, lewd, lascivious] is difficult to establish from the available evidence. It seems most likely that the ‘homosexual’ sense A. 4d [(a) Of a person: homosexual; (b) (of a place, milieu, way of life, etc.) of or relating to homosexuals] was primarily a development of sense A. 4b [Originally of persons and later also more widely: dedicated to social pleasures; dissolute, promiscuous; frivolous, hedonistic. Also (esp. in to go gay): uninhibited; wild, crazy; flamboyant], especially of its connotations of hedonism and lack of inhibition, while the ‘prostitution’ sense A. 4c [euphem. Esp. of a woman: living by prostitution. Of a place: serving as a brothel. Now rare.] was a separate development from sense A. 4b. In quot. 1889 at sense A. 4c^1 gay is used by a male prostitute of people engaged in prostitution, but not specifically in the sense ‘homosexual’ (compare quot. 1890 at sense A. 4c,^2 quoting the same source). Some examples of gay cat n. at Special uses 2a imply a relationship with an older tramp involving sexual favours (compare punk n.1 2b, gunsel n. 1), but this cannot be taken as earlier evidence that gay itself was being used in the sense ‘homosexual’. However, early discomfiture among some homosexuals about the adoption of gay to describe themselves was based on the word's associations both with prostitution (sense A. 4c) and with frivolity and promiscuousness (sense A. 4b): see, for example, discussion by R. R. Butters ‘Cary Grant and the emergence of gay “homosexual”’ in Dictionaries 19 (1998): 188–204.
Support has not been established for statements such as the following about supposed earlier use in French (dictionaries of French only record the sense ‘homosexual’ for gai as a very recent (late 20th-cent.) Anglicism of disputed acceptance, following earlier (unassimilated) borrowing of the English word in this sense as French gay ):
1953 ‘D. W. Cory’ Homosexual Outlook ix. 107 In France as early as the sixteenth century the homosexual was called gaie; significantly enough, the feminine form was used to describe the male. The word made its way to England and America, and was used in print in some of the more pornographic literature after the First World War. Psychoanalysts have informed me that their homosexual patients were calling themselves gay in the nineteen-twenties, and certainly by the nineteen-thirties it was the most common word in use among homosexuals themselves.
By the 1960s gay in the sense ‘homosexual’ (sense A. 4d) had become established as the preferred term of self-reference for many homosexual men. The subsequent more general currency of this sense has led some commentators to claim that this is now the dominant sense of the word, and that gay in its earlier meanings of ‘carefree’ or ‘bright and showy’ cannot readily be used today without at least a sense of double entendre (see discussion in R. W. Burchfield New Fowler's Mod. Eng. Usage (1996) 324).
^1: 1889 J. Saul Statement: Cleveland Street Case (P.R.O. DPP 1/95/4) 38: "I am still a professional ‘Maryanne’. I have lost my character and cannot get on otherwise. I occasionally do odd jobs for different gay people."
^2: 1890 Star 16 Jan. (ed. 5) 2/7: "I worked hard at cleaning the houses of the gay people; the gay ladies on the beat."
EDITS
EDIT: Some formatting, grammatical editing, and to add that Butter's Dictionaries article which the excerpt alludes to is not available in downloadable PDF form. If it is of particular interest to anyone, I recommend using Worldcat to locate the nearest print holdings of that journal, or to inquire at your local university's library about procuring the article through Interlibrary Loan services.
EDIT 2: A source for the Butters article, as provided by u/bcool.
EDIT 3: There was a further question which was deleted which wondered about where the term "faggot" came to refer to homosexuals. I did some more digging in the OED and came up with an interesting theory. I hope it does not derail the original post too much:
The OED’s 6th subsense for the word is defined as “A (male) homosexual. slang (orig. and chiefly U.S.).” And the first citation it provides is dated 1914, from a publication entitled A vocabulary of criminal slang, with some examples of common usages by Louis E. Jackson and C. R. Hellyer. You can search the text of that work here. The word appears in the definition of the word “drag,” which, among other slang definitions from the period, means “female attire donned by a male” in the sense that we still use the term today. Jackson and Hellyer provide an example of usage:
Example: “All the fagots (sissies) will be dressed in drag at the ball tonight.”
How charming! Of course this sheds no light on how the term moved from describing a bundle of sticks to a “sissie,” but the OED might give us a hint at that, too. Interestingly, there is yet another alternative definition for “faggot” listed, “A term of abuse or attempt applied to a woman.” This sense has apparently been in use since the 16th century, where it appears in Thomas Lodge’s 1591 Catharos: Diogenes in His Singularity, which Wikipedia describes as “a discourse on the immorality of Athens”. Lodge writes:
A filbert is better than a faggot, except it be an Athenian she handfull.
The term stayed in usage through the 20th century, where it even appeared in Ulysses wherein is described "That old faggot Mrs. Riordan."
It makes sense that the derogatory term towards women would shift in this way towards effeminate men in the early twentieth century.
As a follow up question: When did "Lesbian" become a slang term for female homosexuals, and does it relate to the Greek Island of Lesbos?
In a comment that clearly did not adhere to the standards in this sub, Cary Grant's use of the term gay was introduced in the movie "Bringing Up Baby" from 1938.
How can we be sure it is meant as a man who enjoys sex with men exclusively? From our pov sure his clothes point towards drag queens, but in that time? Older fashion ideas from the 19th century perhaps?
According to the quote from /u/hairy1ime the word gay developed the meaning it has today from 'uninhibited; wild, crazy; flamboyant.' This could also be used to 'translate' the word to our time, I would think.
So which meaning was used in that scene?
The opposite question is also interesting: How long did people continue to use the word gay for happy? The Flintstones theme song includes the line "gay ol' time". That's the last use I can come up with.
You need to clarify what you mean by homosexual.
In the book "Gay New York: Gender, Urban Culture, and the Making of the Gay Male World, 1890-1940 by George Chauncey" It is stated that the term "gay" comes into popular usage in the 30s (at least in NYC) BUT it only refers to the effeminate men... what had been called "fairies" before. There was a lot of overlap with cross dressing in that group as well.
Other men at the time could have sex with the fairies, but not be considered one of them. Hetero/homo sexual wasn't a distinction made at the time, but the men that did this weren't considered to be "gay", or a "fairy", either by the gay communities or by society. Chauncey gives several examples from interviews and newspapers to back this up. Think sailors or single male immigrants (lack of female options for sexual release).