The story
From gay + -ism. The formation of sense 2 is similar to e.g. transgenderism; the formation of sense 3, "discrimination based on gayness", is similar to e.g. sexism being discrimination based on sex or racism being discrimination based on race.
Etymology from Wiktionary, CC BY-SA 4.0.