Two gay men, one from Iran and another from Cameroon, have won the right to seek asylum in the UK on the ground that they face persecution back home.
A panel of five Supreme Court justices had been asked to decide whether a gay applicant could be refused asylum on the grounds that he could avoid ill treatment by concealing his sexuality.
Previous attempts by the men to stay in the UK had been rejected by judges at the Court of Appeal who ruled that if the men could conceal their sexuality, their situation could have been regarded as "reasonably tolerable".
The judges agreed and ruled that the men's cases could be reconsidered.
Lord Hope, who read out the judgement, said: "To compel a homosexual person to pretend that his sexuality does not exist or suppress the behaviour by which to manifest itself is to deny his fundamental right to be who he is.
"Homosexuals are as much entitled to freedom of association with others who are of the same sexual orientation as people who are straight."
The court said it would be passing detailed guidance to the lower courts about how to treat such cases in the future.
The applicant from Cameroon, who is only identified as HT, had been told he should relocate elsewhere in his country and be "more discreet" in future.
He had been attacked by an angry mob at home after being seen kissing his partner. He has been fighting removal from the UK for the past four years.
"Some people stopped me and said 'we know you are a gay man'," HT earlier told the BBC.
"I cannot go back and hide who I am or lie about my sexuality."
The other application was from a 31-year-old Iranian gay man, who was attacked and expelled from school when his homosexuality was discovered.
Like HT, he had been told he could be "reasonably expected to tolerate" conditions back home that would require him to be discreet and avoid persecution.
Punishment for homosexual acts ranges from public flogging to execution in Iran, and in Cameroon jail sentences for homosexuality range from six months to five years.