It's a victory for Democratic officials who lead the state and city and have traded insults with the US president about his drive to put troops on the ground in major urban areas.
"The court confirmed what we all know: There is no credible evidence of a rebellion in the state of Illinois. And no place for the National Guard in the streets of American cities like Chicago," Governor JB Pritzker said.
The judge said the administration violated the 10th Amendment, which grants certain powers to states, and the 14th Amendment, which assures due process and equal protection.
It wasn't clear what the troops will do now, including a small number outside a US Immigration and Customs Enforcement building in Broadview.
The lawsuit was filed on Monday by Chicago and Illinois as Guard members from Texas and Illinois were on their way to a US Army Reserve Centre southwest of Chicago.
All 500 are under the US Northern Command and had been activated for 60 days.
Some slept in vans on Wednesday night outside the Broadview building, near Chicago, and emerged on Thursday morning on patrol behind portable fences.
For weeks, the ICE site has been the scene of occasional clashes between protesters and federal agents.
Earlier on Thursday, US Justice Department lawyer Eric Hamilton said the Guard's mission would be to protect federal properties and government law enforcers in the field, not "solving all of crime in Chicago".
The nearly 150-year-old Posse Comitatus Act limits the military's role in enforcing domestic laws.
But Trump has said he would be willing to invoke the Insurrection Act, which allows a president to dispatch active duty military in states that are unable to put down an insurrection or are defying federal law.
"Chicago is seeing a brazen new form of hostility from rioters targeting federal law enforcement," Hamilton told US District Judge April Perry.
Perry, however, wasn't swayed.
"I have seen no critical evidence that there is a danger of rebellion in the state of Illinois," she said hours later.
The judge said the order would expire on October 23 at 11.59pm. She set an October 22 hearing by telephone to determine if the order should be extended for another 14 days.
Perry said the actions of the Department of Homeland Security are largely rooted in Trump's "animus toward Illinois elected officials".
She expressed scepticism of the federal government's characterisation of protests in Broadview.
"DHS's narrative of events is simply unreliable," said Perry, who was appointed to the bench by former president Joe Biden.
Chicago Mayor Brandon Johnson sat in a corner of the courtroom, which overflowed into a room with video of the hearing.
The city and state have called the deployments unnecessary and illegal.
"The president does not have the unfettered discretion to turn America's military against its own citizens when they exercise their constitutional rights," Illinois Attorney General Kwame Raoul said.
Trump, meanwhile, has portrayed Chicago as a lawless "hellhole" of crime despite statistics that show a significant drop in offences.
He said Pritzker and Johnson should be arrested for not protecting agents during immigration sweeps. Pritzker's response: "Come and get me."
Also on Thursday, a federal appeals court heard arguments over whether Trump had the authority to take control of 200 Oregon National Guard troops.
The president had planned to deploy them in Portland, where there have been mostly small nightly protests outside an ICE building.
A judge on Sunday granted a temporary restraining order blocking the move.