The Supreme Court on Monday ruled 7-2 in favor of a Colorado baker who refused to make a wedding cake for a gay couple because it went against his religious beliefs.

The justices, in a decision signed by Justice Anthony Kennedy, said Jack Phillips, the owner of Masterpiece Cakeshop in Lakewood, did not violate the state’s anti-discrimination law.

The decision faulted the Colorado Civil Right Commission for violating the Free Exercise Clause of the Constitution by making Phillips bake the cake even though he opposed doing so on religious grounds.

“The laws and the Constitution can, and in some instances must, protect gay persons and gay couples in the exercise of their civil rights, but religious and philosophical objections to gay marriage are protected views, in some instances protected forms of expression,” the majority opinion stated.

The ruling went on to blast comments made during the commission’s hearing that denigrated Phillips’ faith and included remarks that compared his belief in religious freedom to past instances when it was used to justify discrimination like slavery and the Holocaust.

“This sentiment is inappropriate for a commission charged with the solemn responsibility of fair and neutral enforcement of Colorado’s anti-discrimination law — a law that protects discrimination on the basis of religion as well as sexual orientation,” Kennedy said.

Justices Ruth Bader Ginsburg and Sonia Sotomayor dissented.

The Colorado commission said Phillips violated the state’s anti-discrimination law that bars businesses from refusing service based on sex, marital status or sexual orientation by declining to bake a cake in 2012 for David Mullins and Charlie Craig.

“The commission’s hostility was inconsistent with the First Amendment’s guarantee that our laws be applied in a manner that is neutral toward religion,” Kennedy wrote.

The civil rights advocacy group Campaign for Southern Equality said the court didn’t rule that there is a right to discriminate, and the decision doesn’t apply to businesses in other states and doesn’t invalidate anti-discrimination laws that protect gays.

“Here in the South, we see daily evidence of the growing support for LGBTQ equality, even as we continue to face discriminatory laws. We will continue to fight for full equality — and nothing less,” the Rev. Jasmine Beach-Ferrara, the group’s executive director, said in a statement.

Phillips’ lawyer lauded the decision in a statement.

“Government hostility toward people of faith has no place in our society, yet the state of Colorado was openly antagonistic toward Jack’s religious beliefs about marriage. The court was right to condemn that,” said Kristen Waggoner, senior counsel for the Alliance Defending Freedom that argued his case.

The ACLU, which represented Mullins and Craig, said the ruling affirms that businesses cannot discriminate.

“The court reversed the Masterpiece Cakeshop decision based on concerns unique to the case but reaffirmed its longstanding rule that states can prevent the harms of discrimination in the marketplace, including against LGBT people,” ACLU deputy legal director Louise Melling said.

Attorney General Jeff Sessions said the ruling showed the First Amendment bars the government from discriminating over a person’s religious beliefs.

“The Supreme Court rightly concluded that the Colorado Civil Rights Commission failed to show tolerance and respect for Mr. Phillips’ religious beliefs,” Sessions said in a statement. “In this case and others, the Department of Justice will continue to vigorously defend the free speech and religious freedom First Amendment rights of all Americans.”