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

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

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 extended workplace protections to millions of people across the nation, continuing a series of Supreme Court victories for gay rights even after President Trump transformed the court with two appointments.

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