The mayor of London's decision to ban a Christian group's controversial bus advert targeting gay people did not contravene the law, the high court has ruled.

A judge found on Friday that Boris Johnson did not abuse his position as chairman of Transport for London (TfL) last April when he imposed the ban on the advert, which suggested that people could be cured of homosexuality.

The decision is a defeat for the Core Issues Trust, a Christian charity that funds "reparative therapy" for gay Christians, which it claims can "develop their heterosexual potential". They believe Johnson was "politically driven" when he intervened to block the ad. The posters proposed for the sides of the capital's buses read: "Not Gay! Ex-Gay, Post-Gay and Proud. Get over it!"

Mr Johnson condemned the "gay cure" ad as "offensive to gays" and said it could lead to retaliation against the wider Christian community.

Sitting at the high court, Justice Beverley Lang ruled on Friday that TfL's process in introducing the ban "was procedurally unfair, in breach of its own procedures and demonstrated a failure to consider the relevant issues".

However, she said that was outweighed by factors against allowing the ad to be published on buses across the capital. vertisementLang said it would "cause grave offence" to those who were gay and was perceived as homophobic, "thus increasing the risk of prejudice and homophobic attacks". The judge revealed her concern about the issues raised by the case by giving Core Issues permission to appeal to the court of appeal.

The judge said that, although she did not think an appeal would succeed, there were "compelling reasons" to allow an appeal to go ahead.

Lang added that the case "concerns interference with the right to freedom of expression, which is a matter of such fundamental importance that it merits consideration by the appellate court".

Core Issues had asked the judge to rule that the charity was unlawfully denied the freedom to express its views on homosexuality.

Paul Diamond, appearing for the charity, said the ban was imposed "very close" to the mayoral election on 3 May, when Johnson defeated his political opponent Ken Livingstone.

Diamond said: "It was clearly a highly charged issue, and the mayor took credit for the highly politically driven decision.

"The mayor was strongly of the view this advertisement should not run."

Diamond said the Core Issues Trust had nothing but "utter respect for people struggling with same-sex attraction".

He denied that it was attempting to offer a "gay cure" and said the ads were a response to a bus poster campaign by the gay rights group Stonewall, which carried the message: "Some people are gay. Get over it!" Diamond contended the trust was equally entitled to express its view on the sides of buses, and to have its right to freedom of expression protected under article 10 of the European convention on human rights.

TfL refused to carry its ad on the grounds that it was "likely to cause widespread or serious offence to members of the public", and it contained "images or messages which relate to matters of public controversy and sensitivity".

Long standing gay rights campaigner, Peter Tatchell said he did not believe the advert should have been banned from London buses.

Thatchell said the insinuation that gay people "can be cured" was misleading and conversion therapies caused harm, but believed the language of the advert "was not abusive, menacing or threatening".

"Justice Lang, made her decision to uphold the advert ban on the grounds that they are offensive to gay people. She is right. They are offensive, but being offensive is not a legitimate basis for banning anything," he said.

"In a free society there is no right to not be offended. Almost anything that anyone says can potentially be deemed offensive by someone. The law should not cater to the sensitivities of any section of the public. If it did, many adverts, plays, books and films would be banned.

"For decades, gay helplines, youth groups and campaign organisations faced bans on advertising their services.

It is not right for the gay community to turn around and adopt the oppressive, anti-free speech tactics of our past oppressors."