The case was brought by a Palestinian asylum seeker with a liver condition
Failed asylum seekers cannot receive free treatment on the NHS, three judges have ruled at the Court of Appeal.
But hospitals can decide themselves whether to treat such individuals if they have no money, the judges said.
Lord Justice Ward said the patient must have resided lawfully in the UK for at least a year to receive free health service treatment.
The case was brought by a Palestinian asylum seeker, who is under investigation over his asylum claim.
The 35-year-old, identified as YA, claimed asylum in the UK in 2005, but was later refused leave to enter.
YA, who has a liver condition, says he fled his home in 2002 after becoming involved with Hamas and being asked to take part in a political assassination.
Despite claiming asylum in the UK, he was later refused leave to enter.
YA's appeal against that decision was also refused by a judge, who said his primary motivation for coming to Britain was a desire to receive medical treatment and not because of a fear of Hamas.
He was admitted to hospital when his liver condition deteriorated, but was refused free treatment and given a bill for £9,000, which he is unable to pay.
Lord Justice Ward said: "Failed asylum seekers ought not to be here.
"They should never have come here in the first place and after their claims have finally been dismissed they are only here until arrangements can be made to secure their return.
"In some cases, like the unfortunate YA, that return may be a long way off. The result may be most unfortunate for those in ill-health like YA for they may now be at the mercy of the hospitals' discretion whether to treat them or not."
YA took the case over his treatment to the High Court, where a judge declared that government guidance over treatment was unlawful so far as it advised NHS Trusts to charge failed asylum seekers.
The hospital concerned has now agreed to treat YA, but the issue went to the Court of Appeal to decide on the details of the government guidance.
The appeal judges ruled that hospitals do have the discretion to provide free treatment to penniless failed asylum seekers.
Lord Justice Ward said the government's guidance for chargeable patients was to seek a deposit for the full cost of the treatment, but it offered no guidance on what happens when the deposit cannot be paid.
"No help is given in the case of those who cannot return home before the treatment does become necessary. What is to happen to the patient who cannot wait?" he said.
"My conclusion is that it is implicit in the guidance that there is a discretion to withhold treatment but there is also discretion to allow treatment to be given when there is no prospect of paying for it."
'Fear of bills'
Donna Covey, chief executive of the Refugee Council, said: "It cannot be right to deny vulnerable asylum seekers life-saving treatment simply because they are unable to pay for it, and we hope that this ruling will offer extra protection to those who are very sick and vulnerable.
"To refuse treatment to those people simply because they cannot pay for it is appalling and inhumane."
Deborah Jack, chief executive of the National Aids Trust, said she was "very disappointed" by the appeal court's decision.
"There are probably hundreds of thousands of people living in the UK who are unable to access affordable healthcare," she said.
"This undermines social cohesion, increases avoidable illness and death, harms vulnerable children and older people, and contributes to the spread of infectious disease.
"In the short term, much stronger guidance on access to healthcare and debt write-off will be welcome.
"But the fear of bills will continue to deter many people from accessing the care they need, including people who are in fact entitled to free treatment."
Dr Vivienne Nathanson, head of science and ethics at the British Medical Association, said: "There is still a great deal of uncertainty about the lawfulness of providing hospital treatment for this very vulnerable group of people.
"This will only be resolved once the Department of Health re-writes its guidance."