The ruling was made after a case brought by three men from Latvia and Poland, who had been served with deportation notices after being discovered sleeping on the streets.
The orders for removal were based on a rule, introduced by the Home Office in February 2016, that stated sleeping rough could be considered an abuse of EU freedom of movement rights.
Street homelessness was thus regarded as potential grounds to remove European Economic Area nationals or family members from the country.
The policy was illegal under EU law, the courts decided, because sleeping rough did not constitute an abuse of rights. It was also discriminatory, the ruling said.
Paul Heron, a solicitor representing the claimants, told Sky News that the policy had led to a “sweep mentality” that had seen migrants rounded up without their circumstances being investigated.
Mr Heron, from Lambeth Law Centre’s Public Interest Law Unit (PILU), added: “Our clients were either working or had been working, and had fallen on hard times.
“They are coming here to seek work, for a better life. They are not coming here to beg.”
The three men who brought the case, Gunars Gureckis from Latvia, and Mariusz Perlinski and Mariusz Cielecki from Poland, had spent many years in the UK and had been working, seeking work and renting accommodation for much of that time, evidence submitted to the court showed.
A Home Office spokesperson said they were “disappointed” by the ruling, but would not be appealing.
James Eadie QC, who represented the Home Office legal team, argued in court that rough sleeping had been used as an “indicative criteria” for authorities to assess whether someone’s residence in the UK was “genuine” or an “abuse of rights”.
He said a “proportionality assessment” was undertaken if there was evidence of abuse of rights.
Deportations of EU nationals have increased dramatically in recent years.
Some 4,754 EU nationals were deported from the UK in 2016 – compared with 973 in 2010, according to The Independent.
A spokesperson for North East London Migrant Action, which brought the claim with PILU, said: “‘This judgment is a victory for all those who believe that homeless people are human beings with rights rather than simply an eyesore to be eliminated.
“This ruling should make Theresa May’s government think again before introducing policies on immigration that criminalise some of the most vulnerable people in our society.”
In her ruling, Mrs Justice Lang advised Home Secretary Amber Rudd to “take stock” and said ministers had proposed a revised policy that was “more nuanced”.