The Supreme Court on Monday ruled for a Colorado baker who refused to create a wedding cake for a same-sex couple, but it left undecided whether a business owner’s religious beliefs or free speech rights can justify refusing some services to gay people.

Instead, Justice Anthony M. Kennedy’s 7-to-2 decision focused on what he described as religious bias on the part of Colorado Civil Rights Commission members who ruled against baker Jack Phillips, who owns Masterpiece Cakeshop.

“The neutral and respectful consideration to which Phillips was entitled was compromised here,” Kennedy wrote, adding that the commission’s decision that the baker violated the state’s anti-discrimination law must be set aside.

But Kennedy acknowledged that the decision was more of a start than a conclusion to the court’s consideration of the rights of those with religious objections to same-sex marriage and the rights of gay people, who “cannot be treated as social outcasts or as inferior in dignity and worth.”

Future cases that raise those issues “must be resolved with tolerance, without undue disrespect to sincere religious beliefs, and without subjecting gay persons to indignities when they seek goods and services in an open market,” he wrote.

Such a case could come quickly: The court is set to consider this week whether to review a Washington state Supreme Court decision that a florist could not legally decline to provide flowers to a same-sex wedding there.

The bakery case was one of two decisions Monday — a ruling on an immigrant teenager’s abortion was the other — in which the court opted for compromise rather than division. The justices are in the final month of a term that has included contentious fights over President Trump’s power in the travel ban case and what could be a landmark decision on partisan gerrymandering, among other issues.

Kennedy’s narrow ruling drew the support of three of the court’s consistent conservatives — Chief Justice John G. Roberts Jr. and Justices Samuel A. Alito Jr. and Neil M. Gorsuch — and two of its consistent liberals, Stephen G. Breyer and Elena Kagan.

Justice Clarence Thomas agreed with the outcome but said he would have ruled for Phillips on free-speech grounds. Justices Ruth Bader Ginsburg and Sonia Sotomayor dissented, saying comments from the commissioners did not change the outcome of the case.

“What prejudice infected the determinations of the adjudicators in the case before and after the Commission? The Court does not say,” Ginsburg wrote.

In a conference call with reporters, Phillips thanked the court for recognizing “the injustice that the government inflicted on me.” He said that “tolerance is a two-way street” and added, “If we want to have freedom for ourselves, we have to extend it to others with whom we disagree about important issues like the meaning of marriage.”

But James Esseks of the American Civil Liberties Union, which represented the couple at the center of the fight, Charlie Craig and David Mullins, pointed to Kennedy’s protective language about gay citizens and the “general rule” that religious and other objections “do not allow business owners and other actors in the economy and in society to deny . . . equal access to goods and services.”

“The bakery may have won the battle, but it has lost the war,” Esseks said in a conference call with reporters.

Even the dissenting Ginsburg began her opinion: “There is much in the Court’s opinion with which I agree.”

The case presented Kennedy with a dilemma. On the one hand, he is one of the court’s staunchest defenders of free speech and religious freedom. On the other, he has written all of the court’s groundbreaking opinions on gay rights, including that there is a constitutional right for same-sex couples to marry.

He had signaled in oral arguments that comments from members of the commission displaying “religious hostility” might be the narrow way out.

“At several points during its meeting, commissioners endorsed the view that religious beliefs cannot legitimately be carried into the public sphere or commercial domain, implying that religious beliefs and persons are less than fully welcome in Colorado’s business community,” Kennedy wrote.

He noted that one commissioner later said that religious beliefs had been “used to justify all kinds of discrimination throughout history, whether it be slavery, whether it be the holocaust.”

Such statements, Kennedy wrote, “cast doubt on the fairness and impartiality of the Commission’s adjudication of Phillips’ case.”

Phillips contended that dual guarantees in the First Amendment — free speech and the free exercise of religion — protect him against Colorado’s public accommodations law, which requires businesses to serve customers equally regardless of “disability, race, creed, color, sex, sexual orientation, marital status, national origin, or ancestry.”

Alliance Defending Freedom Senior Counsel Kristen Waggoner, who represented Phillips, praised the ruling. “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,” she said.

She and Esseks agreed that the court’s decision resolves this particular case against Phillips. But she was reluctant to opine on what might happen if Phillips again declined to create a wedding cake for a same-sex couple, saying she needed more time to study the ruling.

Across the country, florists, bakers, photographers and others have claimed that being forced to offer their wedding services to same-sex couples violates their rights. Courts have routinely turned down the business owners, as the Colorado Court of Appeals did in the Phillips case, saying state anti-discrimination laws require businesses that are open to the public to treat all potential customers equally.

There’s no dispute about what triggered the court case in 2012, when same-sex marriage was prohibited in Colorado. Craig and Mullins decided to get married in Massachusetts, where it was legal. They would return to Denver for a reception, and those helping with the plans suggested they get a cake from Masterpiece Cakeshop.

The couple arrived with Craig’s mother and a book of ideas, but Phillips cut short the meeting as soon as he learned that the cake was to celebrate the couple’s marriage.

Phillips recalled: “Our conversation was just about 20 seconds long. ‘Sorry guys, I don’t make cakes for same-sex weddings.’ ”

The couple then learned that Colorado’s public accommodations law specifically prohibits discrimination based on sexual orientation, and they filed a complaint with the Colorado Civil Rights Commission. The commission ruled against Phillips, and the appeals court upheld the decision.

On Monday, Craig and Mullins issued a statement on the ruling: “Today’s decision means our fight against discrimination and unfair treatment will continue,” the couple said. “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. 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.”

Within the court’s seven-member majority, there were differences that are likely to animate future cases.

Gorsuch, along with Alito, indicated they would have been willing to go further. Some may find Phillips’s religious beliefs irrational or even offensive, Gorsuch wrote, but that is of no consequence. “In this country, the place of secular officials isn’t to sit in judgment of religious beliefs, but only to protect their free exercise,” he wrote.

He added: “It is no more appropriate for the United States Supreme Court to tell Mr. Phillips that a wedding cake is just like any other — without regard to the religious significance his faith may attach to it — than it would be for the Court to suggest that for all persons sacramental bread is just bread or a kippah is just a cap.”

Kagan, joined by Breyer, said Gorsuch was wrong.

“A wedding cake does not become something different whenever a vendor like Phillips invests its sale to particular customers with ‘religious significance,’ ” Kagan wrote. “As this Court has long held, and reaffirms today, a vendor cannot escape a public accommodations law because his religion disapproves selling a product to a group of customers, whether defined by sexual orientation, race, sex, or other protected trait.”

She referenced a 1968 decision that said a barbecue restaurant owner must serve black customers even though he claimed that his religion did not embrace racial equality.

Monday’s case is Masterpiece Cakeshop v. Colorado Civil Rights Commission.