Gay was one of those substitution words to imply a different meaning, that eventually became the word itself. No one wanted to say "homosexual," so they instead said that some guy was "gay" (happy) around other men. Wink, wink.
Eventually, it got overused to the point it became synonymous with homosexual, and now that's the preferred term. For a while it became a slur, but gay people have reclaimed it over the years. Bigots still try to use it as a slur, but it has very little effect nowadays.