(CNN) Supreme Court Justice Anthony Kennedy tried to make clear on Monday that he was not retreating from his landmark 2015 decision allowing same-sex marriage nationwide, while he sided with a Colorado baker who refused to create a wedding cake for two gay men.

Slowly reading excerpts of his ruling from the court's bench, the bespectacled 81-year-old justice first emphasized the importance of protecting the rights and dignity of gay persons who want to marry.

"Gay couples cannot be treated as social outcasts," said Kennedy, who has been the author of every Supreme Court gay rights ruling since 1996, including the decisive 5-4 ruling in Obergefell v. Hodges three years ago, which opened the door to gay marriage across America.

But Kennedy, cautiously balancing two tenets, stressed that a second principle must also be respected: freedom of religion. In this situation, he said in the 7-2 decision, religion was not.

This was the first major gay-rights case to reach the court since 2015, and an overriding question was how Kennedy, a centrist conservative, would address the competing values. The importance of his voice in this area cannot be overstated. For more than two decades, Kennedy has provided key votes and penned the legal rationale for gay rights.

Read More