A decision of Transport for London (TfL) to ban bus adverts promoting gay conversion therapy was not unlawful, the High Court has ruled.

TfL said it banned the Core Issues Trust adverts in May of last year because they could cause “widespread offence”.

The decision was made just days before the London mayoral election, with Mayor Boris Johnson warning that the ads were “clearly offensive”.

The posters, which were due to run on 24 buses, said: “Not Gay. Ex-Gay, Post-Gay and Proud. Get over it!” and were in response to a previous poster campaign by gay rights charity Stonewall, which said: “Some people are gay. Get over it!”

Last month, Dr Mike Davidson of Core Issues Trust, an anti-gay Christian group, told the High Court that TfL’s decision to pull the adverts was unlawful and “a deep threat” to democratic freedoms.

On Friday, Mrs Justice Lang ruled 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”.

But that was outweighed by factors against allowing the ad, including that it would “cause grave offence” to those who were gay and was perceived as homophobic, “thus increasing the risk of prejudice and homophobic attacks”.

Ben Summerskill, the chief executive of gay rights group Stonewall said many would be pleased by the High Court’s decision.

“Had these voodoo ‘gay cure’ adverts appeared in the pages of the Spectator or the Daily Telegraph it’s unlikely there would have been complaints.

“But in a city where over half of gay young people face bullying at school, and where tens of thousands of gay people are subjected to hate crimes every year just because of the way they were born, it’s perfectly proper for a mayor to object to the use of such advertising in an iconic public setting.”