The Supreme Court ruled Monday that a landmark civil rights law protects gay and transgender workers from workplace discrimination, handing the movement for LGBT equality a stunning victory.

“An employer who fires an individual merely for being gay or transgender defies the law,” Justice Neil Gorsuch wrote for the majority in the 6-3 ruling.

That opinion and two dissents, spanning 168 pages, touched on a host of flash points in the culture wars involving the LGBT community — bathrooms, locker rooms, sports, pronouns and religious objections to same-sex marriage. The decision, the first major case on transgender rights, came amid widespread demonstrations, some protesting violence aimed at transgender people of color.

Until Monday’s decision, it was legal in more than half the states to fire workers for being gay, bisexual or transgender. The vastly consequential decision thus extended workplace protections to millions of people across the nation, continuing a series of Supreme Court victories for gay rights even after President Donald Trump transformed the court with his two appointments.

The decision achieved a decades-long goal of gay rights proponents, one they had initially considered much easier to achieve than a constitutional right to same-sex marriage. But even as the Supreme Court established that right in 2015, workplace discrimination remained lawful in most of the country. An employee who married a same-sex partner in the morning could be fired that afternoon for being gay.

Monday’s lopsided ruling, coming from a fundamentally conservative court, was a surprise. Gorsuch, who was Trump’s first appointment to the court, was joined by Chief Justice John Roberts and Justices Ruth Bader Ginsburg, Stephen Breyer, Sonia Sotomayor and Elena Kagan.

Supporters of LGBT rights were elated by the ruling, which they said was long overdue.

“This is a simple and profound victory for LGBT civil rights,” said Suzanne B. Goldberg, a law professor at Columbia.

Trump said he accepted the ruling. “I’ve read the decision,” he said, “and some people were surprised, but they’ve ruled and we live with their decision.”

The decision will allow people who say they were discriminated against in the workplace based on their sexual orientation or gender identity to file lawsuits, just as people claiming race and sex discrimination may. The plaintiffs will have to offer evidence, of course, and employers may respond that they had reasons unrelated to discrimination for their decisions.