Ashers declined an order placed by gay activist Gareth Lee, claiming the message was inconsistent with their deeply held religious beliefs.
Last year, it was found to have breached equality legislation following a high-profile court case in Belfast.
The appeal was heard before three senior judges at Belfast's Court of Appeal in May, with the reserved judgment delivered on Monday morning.
Delivering the appeal judgment, Northern Ireland's Lord Chief Justice Sir Declan Morgan said: "The fact that a baker provides a cake for a particular team or portrays witches on a Halloween cake does not indicate any support for either."
Lee, a member of the LGBT advocacy group Queer Space, had wanted a cake featuring Sesame Street puppets Bert and Ernie with the phrase "Support Gay Marriage" for a private function marking International Day Against Homophobia.
Through the legal proceedings, Daniel McArthur, the company's general manager, insisted Mr Lee's sexuality was never an issue, rather the message he wanted the bakery to create.
In the original case, District Judge Isobel Brownlie ruled that religious beliefs could not dictate the law and ordered the firm to pay damages of £500.
Ashers, a name with Biblical connotations, has six branches, employs more than 80 people and delivers across the UK and Ireland.
Throughout the legal battle they have been supported by The Christian Institute, which has organised public rallies and garnered financial backing for the case.
Mr Lee's case was taken in conjunction with the Northern Ireland Equality Commission.
In its ruling the court said: "The LGBT community has endured a history of considerable discrimination in this jurisdiction.
"The potential for conflict between the rights of the LGBT community and the religious community has unfortunately long been a feature of public debate in Northern Ireland and the strongest opposition to the decriminalisation of homosexual acts between consenting males came from the religious community.
"It is obviously of importance that the LGBT community should feel able to participate in the commercial life of this community freely and transparently."
It was "direct discrimination", the court said, because the bakers had taken issue with the word "gay" and would have no issue baking a cake that said "Support Heterosexual Marriage".
"This was a case of association with the gay and bisexual community and the protected personal characteristic was the sexual orientation of that community. Accordingly this was direct discrimination," the judges said.
Lee had not put any requirement on the bakers to support gay marriage, the ruling said, and all he wanted was a cake. He had not asked them to agree with or endorse the slogan.
At the end of the judgement the Equality Commission, which brought the case against Ashers, came in for criticism.
It was accused of not being interested in protecting faith communities. It had not taken appropriate steps to give advice to Ashers at an earlier stage.
"We would hope that such a course would be followed if a situation such as this were to arise in future," the ruling said.
Peter Lynas from the Northern Ireland Evangelical Alliance told Premier: "The reality is that the Commission have not been overly helpful here in some of the statements they have made.
"There will be concern until they rebuild relationships with the faith sector and really explain what the outcome of this decision means for businesses.
"Some are going to be really nervous now".
Michael Wardlow's the Chief Commissioner of the Equality Commission for Northern Ireland.
He told Premier that accusations it favoured the gay community are untrue: "They need to have evidence in that. Just because you say it doesn't make it fact.
"The fact that we've only taken up two per cent of LGBT cases speaks for itself."
Outside court, Daniel McArthur said he was "extremely disappointed" by the ruling.
Standing with his father Colin, mother Karen and wife Amy, he said the family would be taking legal advice on whether there was a way to appeal against the judgment.
"If equality law means people can be punished for politely refusing to support other people's causes then equality law needs to change," he said.
"This ruling undermines democratic freedom, it undermines religious freedom and it undermines free speech."
Thanking all the family's supporters, he ended his comments with a passionate declaration of his Christian faith.
"It's been a trying time but we are thankful to God and his faithfulness to us through everything - he is still on the throne, he is the ruler of heaven and of earth and he is our God and we worship and we honour him," he said.
Gay rights campaigner Peter Tatchell criticised the Appeal Court ruling and said it set a "dangerous, authoritarian precedent", claiming gay bakers could now theoretically be compelled to bake cakes carrying homophobic slogans.
While he profoundly disagrees with Ashers' views on gay marriage, he said he supports the Christian bakers' contention that they should not be forced to endorse a message that runs contrary to their beliefs.
"This verdict is a defeat for freedom of expression," he said.
"It seems that businesses cannot now lawfully refuse a customer's request to propagate a message, even if it is a sexist, xenophobic or anti-gay message and even if the business has a conscientious objection to it.
"Although I strongly disagree with Ashers' opposition to marriage equality, in a free society neither they nor anyone else should be compelled to facilitate a political idea that they oppose."