The Equality Commission for Northern Ireland brought the case against Ashers Bakery on behalf of gay rights activist Gareth Lee whose order was refused.
The family-run bakery, which delivers across the UK and Ireland, turned down the request for a cake with an image of Sesame Street puppets Bert and Ernie below the motto Support Gay Marriage.
It had been ordered for a private function marking International Day Against Homophobia last May.
Making the ruling Judge Brownlie said: "The defendants have unlawfully discriminated against the plaintiff on grounds of sexual discrimination.
"This is direct discrimination for which there is no justification."
Evidence in the landmark ruling, which generated headlines around the world and divide opinion across Northern Ireland, was heard over three days in March.
It began when Mr Lee placed the order at the Belfast branch of Ashers' he paid in full and said he was left stunned when two days later the company phoned to say it could not be processed.
In his evidence, Mr Lee claimed to have been left feeling like a lesser person.
Judge Brownlie said the McArthur family held "genuine deeply held religious beliefs" but said that they must have been aware that Mr Lee was gay and were aware of the ongoing same sex marriage debate because of the graphics he had supplied.
She said: "As much as I acknowledge their religious beliefs this is a business to provide service to all. The law says they must do that."
District Judge Brownlie accepted he (Mr Lee) had been treated "less favourably", contrary to the law.
She said: "My finding is that the defendants cancelled this order as they oppose same-sex marriage for the reason that they regard it as sinful and contrary to their genuinely-held religious beliefs.
"Same-sex marriage is inextricably linked to sexual relations between same-sex couples, which is a union of persons having a particular sexual orientation. The plaintiff did not share the particular religious and political opinion which confines marriage to heterosexual orientation.
"The defendants are not a religious organisation. They are conducting a business for profit and, notwithstanding their genuine religious beliefs, there are no exceptions available under the 2006 regulations which apply to this case."
Among the Christian supporters in the courtroom was former Northern Ireland health minister Edwin Poots and DUP Lagan Valley MLA Paul Givan.
Throughout the lengthy hearing, Ashers general manager Daniel McArthur sat beside his wife Amy in the well of the court alongside other family members. The couple smiled as the judgment was read out in full.
Speaking earlier, the 25-year-old said faith had helped sustain his family through a "difficult" time.
Mr Lee sat impassively on the other side of the dock, flanked by male and female friends.
The judge told the court she believed that if a heterosexual person had ordered a cake with graphics promoting "heterosexual marriage" or simply "marriage", the order would have been fulfilled.
"I have no doubt such a cake would have been provided. It is the word 'gay' to which the second and third defendants took exception, the connotation of gay marriage which the defendants regarded as sinful."
District Judge Brownlie said: "They (Ashers) are in a business supplying services to all. The law requires them to do just that."
She later added: "They were contracted on a commercial basis to bake and ice a cake. The plaintiff was not seeking support or endorsement."
Both parties agreed that £500 of damages are to be awarded to Mr Lee.
Speaking outside the court DUP MLA Paul Givan who is seeking to bring forward a so-called conscience clause which would allow businesses to refuse to provide services they believed could compromise their religious beliefs, said the Equality Commission should apologise.
He said: "This is an assault on faith.
"Are Christians going to be dragged through the courts? I don't believe the people of Northern Ireland want that.
"The Equality Commission should apologise. I hope this case will be appealed."
Speaking on behalf of Mr Lee, his lawyer said he would be making a decision as to what charity he will donate the money from the damages.
"It is difficult being a pathfinder in a discrimination case."
While Mr Lee did not publicly speak to the media about the case his lawyer said he is delighted.
Speaking after the ruling the owner of the bakery Daniel McArthur supported by his wife Amy said the bakery said he was "extremely disappointed" by the decision but stated that they would not be closing down.
He said: "We want to say thank you to the thousands of people who supported us.
"Most of all we are thankful to God who has been faithful to us throughout all of this as we carry this burden in the future"
The McArthur family, who employ 80 staff across nine branches and deliver across the UK and Ireland, have been supported by the Christian Institute, which has paid their legal fees.
Karen McArthur, a founder and company director at Ashers, told the court she had accepted the request to avoid embarrassment or confrontation but, as a born-again Christian, knew she could not fulfil it.
The high-profile case divided public opinion in Belfast and beyond.
In response to the ruling the Presbyterian Church expressed "profound concern" and said "it is clear the law needs to be changed".
In a statement Very Rev. Dr. Norman Hamilton, Convener of the Presbyterian Church in Ireland’s Council for Church in Society said: “Today’s ruling should profoundly concern everyone who wants to live in a genuinely pluralist society.
“The owners of Ashers Bakery consistently maintained that the issue for them lay with the message they were asked to produce and not with the customer.
"Yet in a deeply disturbing development, businesses will now be compelled to produce materials or messages, even if they are incompatible with their owners’ deeply and reasonably held beliefs.
“Such levels of state control are surely contrary to the freedoms and liberties that we should seek to uphold out of respect for the worth of every person and for the common good of society.
“In light of today’s judgement, it is clear that the law needs to be changed. In a truly pluralist society, the law should protect everyone from discrimination, while properly valuing the role of conscience. No one should be treated as a second class citizen, as we are all created with equal worth in the eyes of God.
“This is why as a Church we have stated that we want to see the concept of ‘reasonable accommodation’ developed in law. This would achieve a better way for people of all faiths and none to manifest their beliefs in the public arena, which includes the business world.
“Whilst we recognise that this case has been a lengthy and painful experience for all of those directly involved, and troubling for wider society, we believe that there is an urgent need for an informed, gracious and comprehensive discussion about the role of conscience in the public square.
“No doubt we, along with many others, will be giving further consideration to this judgement and its implications in the days ahead.”
Northern Ireland Deputy First Minister Martin McGuinness tweeted: "Ashers bakery judgement a good result for equality, gay people have for far too long been discriminated against. We and the law on their side."
John O'Doherty from gay rights group the Rainbow Project said that while some people might be sympathetic to the position in which the company finds itself, it does not change the facts.
"The judge clearly articulated that this is direct discrimination for which there can be no justification."
Same-sex marriage remains a contentious issue in Northern Ireland and attempts to have it legalised have been rejected four times by the devolved Assembly at Stormont.
The cake row has prompted a proposal to include a so-called "conscience clause" in equality legislation - a move Sinn Fein has vowed to veto.
Here is our live feed from Belfast Telegraph reporter Deborah McAleese @DeborahMcAleese :
"We will not be closing down. We have not done anything wrong," Daniel McArthur
Gareth Lee will be donating his £500 to charity his lawyer said
"The equality commission should apologise. I hope this case will be appealed." Paul Givan
Are Christians going to be dragged through the courts? I don't believe the people of NI want that" Paul Givan
Mr Givan " are we going to drag other families through the courts because of their deeply held religious beliefs?"
Paul Givan says there's huge disappointment in the judgement
Both parties agree to £500 damages be paid to Mr Lee
"I give in favour of the plaintiff" £500 damages requested
"The defendants are entitled to hold and manifest their religious beliefs but in accordance with the law"
She said to do otherwise "would be to allow religious belief dictate what the law is"
Judge says the extent of 2006 regulations and 1998 order "limit the manifestation of the defendants' religious beliefs"
"The law on NI prohibits the defendants from acting as they did" judge tells court
Sexual orientation is a highly protected right under ECHR as is religion" #Ashers
Judge says a secular judge must be very wary of crossing the divide between the Church and state
Judge says there are competing human rights in this case #AshersBakery
"Whilst defendants have right to religious beliefs they are limited as to how they manifest them" judge says
Considering ECHR Article 9 Judge says the defendants "have a Christian belief that's sincerely held"
Judge says even if persuaded Ashers were unaware of Mr Lee's political beliefs she still would've found they treated him less favourably
Judge says Ashers directly discriminated against Gareth Lee on political or religious grounds
Judge says the defendant must have known the plaintiff (Gareth Lee) supported gay marriage
Judge now considering if Ashers discriminated on grounds of political opinion or religious beliefs
Judge says Gareth Lee was discriminated against on grounds of sexual orientation #Ashers
This is direct discrimination for which there is no justification" #Ashers
The defendants have unlawfully discriminated on the grounds of sexual orientation
"The defendants are not a religious organisation. They are a business for profit. There are no exceptions available"
"As much as I acknowledge their religious beliefs this is a business to provide service to all. The law says they must do that"
"I believe the defendants did have the knowledge that the plaintiff was gay"
"The defendants are not a religious organisation. They conduct a business for profit"
Government regulations "protect people from having their sexual orientation used for having their business turned down"
Judge says McArthurs "hold genuine deeply held religious beliefs"
Judge tells court McArthurs said because of their Christian beliefs they could not promote same sex marriage
"The defendants are Christians & regular church goers. They said they seek to live at all times in accordance with the doctrines of Bible"
Gareth Lee claims he has been discriminated against contrary to Equality Act & Fair Employment, judge says
Judge says the defendants deny they have discriminated unlawfully & say they are entitled to refuse an order on grounds of religious belief
Gay rights activist Gareth Lee has arrived in the courtroom #Ashers
"We have been sustained throughout by the knowledge God is faithful and in control"
"We have always served everyone we cannot promote a cause that goes against what the bible says about marriage"
"Because of the Equality Commission's decision we have endured many anxious months"
"We have tried to be guided in our actions by our Christian beliefs" Daniel McArthur tells the media outside Laganside Courthouse
"We will not promote a cause against what the bible says about marriage" Daniel McArthur
Daniel McArthur says his family have been "sustained by the knowledge God is faithful"