"Gay", on its face, is not a derogatory term. It's typically preferred by those to whom it applies over the term "homosexual", the technical nature of which makes this state of being sound like a communicable disease or mental illness (which, as recently as the DSM-II, seventh edition, it was, categorized as a pathological phobia of the opposite sex). In common parlance it is used more to refer to men (the term "lesbian" being gender-specific to women) but can be applied to either gender. Many advocacy groups, such as GLAAD (Gay/Lesbian Association Against Defamation) and terms they promote for use in the media (such as LGBT) incorporate this term, though this isn't to say the terms will always be acceptable for this reason; the group name often persists long after the words behind them have fallen from favor, to avoid the confusion of a rebranding of the group.
Now, any identifying term for a subset of the human population, especially those identifying a trait of the subset not shared by the majority, can be derogatory. It can be said in a condescending way, or in printed form it can be used in an unflattering context. The same words, on the other hand, can also come to be "owned" by the group of people to which the term refers, and when said by a member of that group to refer to themselves, it's acceptable to some degree even if the term is extremely offensive if said by anyone outside that subset.
These can be known as "inside" terms, whose use is only acceptable when used by a member of the group to refer to themselves or another. Many people will know the one I'm pointedly avoiding, referring to African-Americans, but this term is not unique in that category. "Queer", an old slang term for gay men, and "fag"/"faggot", originally AE slang for a wimp or unmanly person, but which came to be associated with the stereotypical "swishy" gay man, have become inside terms; still inappropriate when used by a neutral party or a "straight" person, but acceptable in context between gay men.