Matthew Aplin, 42, the former head at Tywyn primary in Neath Port Talbot, was found to have been discriminated against in a tribunal panel ruling.
School governors had appealed against that decision but it has been upheld by the Employment Appeals Tribunal (EAT).
Judge Shanks, in a written judgement, dismissed the school’s appeal.
In his ruling, the judge said “openly gay” Mr Aplin met the two teenagers in August 2015 via the Grindr dating app and the three of them had sex, but within days police and local social services became aware of what had taken place.
School governors held a disciplinary hearing in May 2016 and decided he should be sacked immediately on the basis his judgement had undermined their trust and confidence in him.
Mr Aplin, who had been a teacher for 19 years, appealed against the decision but resigned before his appeal was heard.
He later started his employment tribunal claim.
That tribunal, sitting in Cardiff in September 2017, ruled he was unfairly constructively dismissed and was discriminated against by the school’s investigating officer, who worked for Neath Port Talbot Council, on the basis he was gay.
The school’s governing body appealed to the EAT, claiming the investigating officer’s approach had not discriminated against Mr Aplin because he was gay.
At the same time, Mr Aplin cross-appealed against a finding by the original tribunal that the governors themselves had not directly been discriminatory against him.
Judge Shanks allowed Mr Aplin’s cross-appeal in relation to his claims against the governors, saying the original tribunal had not properly scrutinised the position of the governors.
The judge ordered the same tribunal panel reconsider whether the governors, in addition to the investigating officer, had discriminated against Mr Aplin.