The Supreme Court sided with the Colorado baker who refused to make a custom cake for a same-sex wedding in a narrow ruling on Monday.

In a 7-2 decision written by Justice Anthony Kennedy, the court said the Colorado Civil Rights Commission violated the free exercise clause of the Constitution when it ruled Jack Phillips had broken the state's public accommodations law in refusing to make a couple a custom cake even though he morally and religiously opposes same-sex marriage.

In its majority ruling, the court said the laws and the Constitution can, and in some instances must, protect gay persons and couples in the exercise of their civil rights, but religious and philosophical objections to same-sex marriage are protected views and in some instances protected forms of expression.

The court said Colorado law can protect gay people from being discriminated against by businesses that are open to the public, but the law must be applied in a manner neutral toward religion.

Since the court based its ruling on the state commission's treatment of Phillips, the decision did not provide the sweeping victory for religious rights that some backing Phillips hoped to see. Phillips had originally asked the court to rule that wedding cakes are an artistic expression of speech and religion protected by the First Amendment, but the court did not go that far.

Kennedy specifically noted court precedent, writing that states are well within their power to enact anti-discrimination measures in public accommodations laws when the legislature believes a given group is the target of discrimination.

But he said the commission showed a clear and impermissible hostility toward the sincere religious beliefs motivating Phillips's objections to making the custom cake when it heard the dispute in 2014.

Kennedy said one commissioner on the panel during a public hearing disparaged Phillips's faith as "despicable" and another member compared his sincerely held religious beliefs to defenses of 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 wrote.

If a member of the clergy were to refuse to perform a same-sex marriage, Kennedy said that would be understood to be a protected exercise of religion "that a gay person could recognize and accept without diminishment to their own dignity and worth."

But, he said, "if that exception were not confined, then a long list of persons who provide goods and services for marriages and weddings might refuse to do so for gay persons, thus resulting in a community-wide stigma inconsistent with the history and dynamics of civil rights laws that ensure equal access to goods, services and public accommodations."

Any decision in favor of the baker, he said, would have to be constrained or else all business owners who object to same-sex marriages for moral or religious reasons would be allowed to refuse their services.

Kennedy was joined by justices across the spectrum, including Chief Justice John Roberts, fellow conservative Justices Samuel Alito and Neil Gorsuch, and liberal Justices Stephen Breyer and Elena Kagan.

Liberal Justice Ruth Bader Ginsburg wrote a dissenting opinion, which fellow liberal Justice Sonia Sotomayor joined.

Ginsburg said the comments of one or two members of Colorado's Civil Rights Commission don't justify reversing a lower court's decision siding with the couple, Charlie Craig and David Mullins.

"Whatever one may think of the statements in historical context, I see no reason why the comments of one or two Commissioners should be taken to overcome Phillips' refusal to sell a wedding cake to Craig and Mullins," she wrote.

Conservative Justice Clarence Thomas argued in a concurring opinion that a wedding cake is an expressive form of speech that should be protected under the First Amendment and that the Colorado Court of Appeals was wrong to conclude otherwise.

He said he warned his colleagues when they legalized same-sex marriage in the 2015 Obergefell v. Hodges case that the decision would inevitably come into conflict with religious liberty.

"This case proves that the conflict has already emerged," Thomas wrote.

"Because the Court's decision vindicates Phillips' right to free exercise, it seems that religious liberty has lived to fight another day. But, in future cases, the freedom of speech could be essential to preventing Obergefell from being used to 'stamp out every vestige of dissent' and 'vilify Americans who are unwilling to assent to the new orthodoxy,' " he wrote.

The narrow ruling mollified some critics of the decision.

"I think that the bakery got a get out of jail free card today because of what the court thought of as misbehavior by the Colorado Civil Rights Commission, but that doesn't mean they get to discriminate in the future. Not at all," James Esseks, director of the LGBT & HIV Project at the American Civil Liberties Union, who represented the couple, told reporters.

Esseks said he reads the court's decision as limited to this specific case.

"If a new same-sex couple walks in that business, I see no reason in this opinion that Masterpiece Cakeshop is free to turn them away," he said.

The commission had ordered Phillips to make cakes for other same-sex weddings, educate his staff about state civil rights laws and file quarterly compliance reports with the commission for two years.

His attorney, Alliance Defending Freedom senior counsel Kristen Waggoner, argued that Phillips lost 40 percent of his business and most of his staff for standing up for what he believes.

Waggoner read the court's decision as farther reaching than opposing groups.

"It's a 7 to 2 ruling. That's a broad ruling and the court was very clear multiple times in its decision that the government was wrong to punish Jack and cannot express hostility towards people of faith," she said.

"In fact that hostility was so overt that the court just blanketly reversed that decision."

Waggoner said the question over what services constitute religious expression protected under the First Amendment will reach the court in the future.

In fact, the Supreme Court already has a case waiting in the wings.

Last July, a Washington state flower shop owner appealed to the high court after the Washington Supreme Court held she violated the state's anti-discrimination law by refusing to create custom floral arrangements for the same-sex marriage of a longtime customer. Barronelle Stutzman, who owns Arlene's Flowers Inc., argues her arrangements are an artistic expression protected by the First Amendment.

Court watchers say the court could use this case to squarely resolve the religious liberty and anti-discrimination claims raised in the Masterpiece Cakeshop case. The case has been re-listed for discussion at the court's next conference on Thursday.

House Minority Leader Nancy Pelosi (D-Calf.) said she would seek legislation to protect gay rights in response to Monday's decision.

"The Masterpiece Cakeshop case is about the most fundamental right of all Americans: to be free from persecution and discrimination because of who they are or whom they love. While narrowly framed to apply to the decision-making process undertaken by the state commission, today's wrongheaded decision fails to uphold equality in this case," she wrote in a statement.

Gregory Angelo, president of Log Cabin Republicans, a prominent LGBT group on the right, said the court's decision shines a spotlight on the glaring absence of federal LGBT nondiscrimination legislation.

"The fact that Justices appointed by Presidents Clinton and Obama sided with Masterpiece Cakeshop should be an indication that there is more nuance to this decision than meets the eye," he said in a statement.

Craig and Mullins, the couple in the case, said the decision means their fight against discrimination will continue.

"We have always believed that in America, you should not be turned away from a business open to the public because of who you are," they said. "We brought this case because no one should have to face the shame, embarrassment, and humiliation of being told 'we don't serve your kind here' that we faced, and we will continue fighting until no one does."

Updated at 4:08 p.m.