The Supreme Court ruled Monday that federal anti-discrimination laws protect gay and transgender employees, a major gay rights ruling written by one of the court’s most conservative justices.

Justice Neil M. Gorsuch and Chief Justice John G. Roberts Jr. joined the court’s liberals in the 6 to 3 ruling. They said Title VII of the Civil Rights Act of 1964, which prohibits discrimination “because of sex,” includes LGBTQ employees.

“Today, we must decide whether an employer can fire someone simply for being homosexual or transgender. The answer is clear,” Gorsuch wrote. “An employer who fires an individual for being homosexual or transgender fires that person for traits or actions it would not have questioned in members of a different sex. Sex plays a necessary and undisguisable role in the decision, exactly what Title VII forbids.”

For 50 years, courts read that to mean only that women could not be treated worse than men, and vice versa, not that discrimination on the basis of sex included LGBTQ individuals.

The court combined two cases to consider whether gay workers are protected under the law. Gerald Bostock claimed he was fired from his job as a social worker in Clayton County, Ga., after he became more open about being gay, including joining a gay softball league. Donald Zarda said he was fired as a skydiving instructor after joking with a female client to whom he was strapped for a tandem dive that he was gay. (Zarda died in 2014.)

The transgender case was brought by Aimee Stephens, who worked for years at a Michigan funeral home before being fired after informing the owners and colleagues of her gender transition. Stephens died of kidney failure in May, after seeing her case argued at the Supreme Court in October.

The cases were the first the court heard since the retirement of Justice Anthony M. Kennedy. He had written the majority opinion in all of the court’s major cases that advanced gay rights, including the 2015 decision that said gay couples had the constitutional right to marry.

The issue was one of the most consequential of the term. More than 70 friend-of-the-court briefs were filed, dividing states, religious orders and members of Congress. More than 200 of the nation’s largest employers are supporting the workers.

The Trump administration sided with the employers, a position that put it at odds with the Equal Employment Opportunity Commission, which decided in 2015 that gay and transgender individuals were federally protected.

Treating a man who is attracted to men differently from a woman who is attracted to men is discrimination, the EEOC reasoned.

The commission also looked at a 1989 Supreme Court decision that said federal law protected against discrimination based on stereotypes; the court found for a woman who had not been promoted because her employers found her too aggressive and her manner of dress not feminine enough.

That is analogous to discriminating against a transgender individual for not conforming to norms expected of a gender, the commission said. Discrimination because of sexual orientation is the same thing, the EEOC said, because it relies on stereotypes about to whom men and women should be attracted.

Most courts of appeals had come to agree with the EEOC, even when they had not done so in the past.

The full U.S. Court of Appeals for the 2nd Circuit ruled for Zarda, and said its contrary past decisions on the issue were wrong.

Chief Judge Robert A. Katzmann wrote that “sexual orientation discrimination is motivated, at least in part, by sex and is thus a subset of sex discrimination.” (Zarda’s case is being carried forward by his sister and partner.)

The U.S. Court of Appeals for the 6th Circuit came to a similar conclusion in Stephens’s case. The panel found it “analytically impossible to fire an employee based on that employee’s status as a transgender person without being motivated, at least in part, by the employee’s sex.”

But in Bostock’s case, the U.S. Court of Appeals for the 11th Circuit went the other way, ruling for Clayton County, a suburb south of Atlanta, that Title VII did not protect on the basis of sexual orientation.

Gay rights leaders say “married on Sunday, fired on Monday” is a possibility in more than half of the United States, where there is no specific protection for gay or transgender workers. The states that prohibit discrimination are not uniform — some protect only gender identity or transgender status, and some differentiate between public and private employment.

Since the case was argued, Virginia became the most recent state to extend protection on its own.

The sexual orientation cases are Bostock v. Clayton County, Ga. and Altitude Express v. Zarda. The other case is R.G. & G.R. Harris Funeral Homes v. EEOC.