Felix Ngole, a religious education teacher from Barnsley, said homosexuality was a sin during an online discussion on Facebook and was removed from his social work postgraduate degree course at the University of Sheffield over his views.
Mr Ngole said he was lawfully expressing a traditional Christian view and has complained that bosses at the University of Sheffield unfairly stopped him completing his studies.
But Deputy High Court judge Rowena Collins Rice said she believed university bosses had acted within the law.
Mr Ngole had argued that his rights to freedom of speech and thought, enshrined in the European Convention on Human Rights, had been breached.
But lawyers representing the university argued that he showed 'no insight' and said the decision to remove him from the course was fair and proportionate.
They said Mr Ngole had been studying for a professional qualification, adding that university bosses had to consider his fitness to practise.
Judge Collins Rice said freedom of religious discourse was a public good of great importance.
But she said social workers had considerable power over the lives of vulnerable people and said trust was a precious professional commodity.
She said that after weighing the facts of this case she decided to dismiss Mr Ngole's claim for judicial review.
Mr Ngole posted comments two years ago, when in his late 30s, the judge had been told.
He was taking part in an debate on a Facebook page about Kim Davis, a state official in the US state of Kentucky, who refused to register same-sex marriages.
Mr Ngole said he had argued that Mrs Davis's position was based on the 'Biblical view of same-sex marriage as a sin'.
He said he was making a 'genuine contribution' to an important public debate and said he was 'entitled to express his religious views'.
University bosses said he had posted comments on a publicly accessible Facebook page which were 'derogatory of gay men and bisexuals'.
"Public religious speech has to be looked at in a regulated context from the perspective of a public readership," said Judge Collins Rice.
"Social workers have considerable power over the lives of vulnerable service users and trust is a precious professional commodity."
The judge added: "Universities also have a wide range of interests in and responsibilities for their students - academic social and pastoral.
"Where, as Sheffield does, they aspire to be welcoming environments for students from a diverse range of backgrounds, they must expect to be inclusive and supportive of that diversity."
She went on: "Where courses leading to professional registration are concerned, universities have an additional set of responsibilities to their students.
"They must teach and support them to be ready to take up demanding roles in the delivery of public services."
Mr Ngole said he planned to appeal and added: "I am very disappointed by this ruling, which supports the university's decision to bar me from my chosen career because of my Biblical views on sexual ethics.
"I intend to appeal this decision, which clearly intends to restrict me from expressing my Christian faith in public."