Five justices at the UK's highest court unanimously ruled on Wednesday that the McArthur family did not discriminate against gay rights activist Gareth Lee on the ground of sexual orientation.
The court's president, Lady Hale, said: "This conclusion is not in any way to diminish the need to protect gay people and people who support gay marriage from discrimination.
"It is deeply humiliating, and an affront to human dignity, to deny someone a service because of that person's race, gender, disability, sexual orientation, religion or belief.
"But that is not what happened in this case."
Announcing the court's ruling on Mr Lee's claim against Ashers bakery, Lady Hale said: "As to Mr Lee's claim based on sexual discrimination, the bakers did not refuse to fulfil his order because of his sexual orientation.
"They would have refused to make such a cake for any customer, irrespective of their sexual orientation."
The judge added: "The bakers could not refuse to supply their goods to Mr Lee because he was a gay man or supported gay marriage, but that is quite different from obliging them to supply a cake iced with a message with which they profoundly disagreed."
Dr Michael Wardlow, chief commissioner of the Equality Commission for Northern Ireland, said the judgment "may raise uncertainty" about what businesses can do and what customers may expect.
It also raises the prospect that "the beliefs of business owners may take precedence over a customer's equality rights, which in our view is contrary to what the legislature intended", he said.
The Rainbow Project, Northern Ireland's largest support organisation for lesbian, gay, bisexual and transgender (LGBT) people, expressed its disappointment at the Supreme Court ruling.
Director John O'Doherty said: "Ashers agreed to make the cake. They entered into a contractual agreement to make this cake and then changed their mind.
"While sympathetic as some may be to the position in which the company finds itself, this does not change the facts of the case.
"We believe this is direct discrimination for which there can be no justification. We will however take time to study this judgment by the Supreme Court to understand fully its implications for the rights of LGBT people to access goods, facilities and services without discrimination.
"We do not believe that this matter should have been brought to court. We believe that Ashers bakery should have accepted the Equality Commission's invitation to engage in mediation, where a remedy could have been found without the expense and division surrounding this court case.
"However, most damaging of all has been the attempt by politicians to use this case to justify amending the law to allow businesses to discriminate against LGBT people with the so-called 'conscience clause.'"
Outside court, Ashers general manager Daniel McArthur said: "I want to start by thanking God. He has been with us during the challenges of the last four years. Through the Bible and the support of Christians, He has comforted us and sustained us. He is our rock and all His ways are just.
"We're delighted and relieved at today's ruling. We always knew we hadn't done anything wrong in turning down this order. After more than four years, the Supreme Court has now recognised that and we're very grateful. Grateful to the judges and especially grateful to God.
"We're particularly pleased the Supreme Court emphatically accepted what we've said all along - we did not turn down this order because of the person who made it, but because of the message itself.
"The judges have given a clear signal today. In fact it couldn't be any clearer. Family businesses like ours are free to focus on giving all their customers the best service they can - without being forced to promote other people's campaigns.
"I know a lot of people will be glad to hear this ruling today, because this ruling protects freedom of speech and freedom of conscience for everyone.
"On behalf of my family can I say thank you to everyone who has supported us or prayed for us through all this.
"We want to move on from this now, and I'm sure Mr Lee does as well. And let me finish by saying that he will always be welcome at any of our shops."
Speaking outside the Supreme Court after the judgment was handed down, Gareth Lee said: "To me, this was never about a campaign or a statement. All I wanted was to order a cake in a shop that sold cakes to order.
"I paid my money, my money was taken and then a few days later it was refused.
"That made me feel like a second-class citizen.
"I'm concerned not just for the implications for myself and other gay people, but for every single one of us."
DUP leader Arlene Foster said the judgment was "historic and seminal".
She tweeted: "This has been a long journey for everyone involved in the case. I commend Amy & Daniel McArthur for their grace and perseverance. This now provides clarity for people of all faiths and none."