The case applies to failed asylum seekers who are waiting to leave the country
Regulations banning failed asylum seekers from receiving free NHS treatment have been declared unlawful by a High Court judge.
The decision could affect up to 11,000 people who are waiting to be sent back to their home countries.
Mr Justice Mitting made the ruling in a test case of a Palestinian who claimed denying care for his chronic liver disease breached his human rights.
The Department of Health has been given permission to appeal.
People who are unable to return home because of travel restrictions or because they are too ill have until now been banned from free NHS treatment.
These people are deemed to be "ordinarily resident", which means their return home has been delayed for over a guideline period of more than six months.
The judge said the existing guidance was unlawful because the definition of "ordinarily
resident" was not restricted in time and authorities had a discretion as to who qualified for "ordinarily resident" status.
'Stuck in the UK'
The man whose case brought the issue to the High Court is unable to return to the West Bank owing to travel restrictions and problems over documentation.
In his 30s and known only as A, he applied for asylum when he arrived in England three years ago. His case was rejected and he agreed to return to the West Bank
The Home Office provides him with accommodation and gives him £35 per week to live on.
But his local London hospital has refused to treat him as officially he is a failed asylum seeker, although he has been cared for while the case is heard.
Adam Hundt, of human rights specialists Pierce Glynn and who represented the man, said the rules were leading to "grotesque human suffering".
"My client is effectively stuck in the UK, even though he is doing all that he can to return home.
"He has never broken the law, and the Home Office recognises that it has to provide him with accommodation so as not to breach his human rights.
"It seems perverse that housing is considered a basic human right and that health care is not."
The Terrence Higgins Trust said the ruling would benefit those failed asylum seekers with HIV.
Lisa Power, head of policy for THT said: "The outcome of this ruling is a sensible, humane decision for many people we work with.
"If someone is living in the UK, they should be treated the same as any other resident."
And Deborah Jack, chief executive of the National Aids Trust, which helped bring the case to court, said: "For years failed asylum seekers have been denied free treatment for long term conditions including HIV.
"Many have faced enforced ill-health as government policy has left them destitute and without health care."