US District Court Judge William Orrick issued a permanent injunction Monday blocking Trump's executive order seeking to strip so-called sanctuary cities of federal funding.
The ruling represents a major setback to the administration's attempts to clamp down on cities, counties and states that seek to protect undocumented immigrants who come in contact with local law enforcement from deportation by federal authorities.
The ruling was also the latest instance in which a federal judge has stood in the way of Trump's effort to implement his hardline policies immigration, joining rulings that have blocked different portions of Trump's travel ban and preliminary injunctions on the sanctuary cities order.
Monday's ruling, which followed lawsuits from two California counties, nullifies Trump's January executive order on the matter, barring the administration from setting new conditions on spending approved by Congress.
The January order sought to crack down on jurisdictions such as Chicago, New York, Los Angeles and San Francisco that do not comply with Immigration and Customs Enforcement (ICE) requests for assistance with identifying and deporting undocumented immigrants.
It was not immediately clear whether the Trump administration was preparing to appeal the ruling, but the Justice Department said it plans to "vindicate the President's lawful authority to direct the executive branch."
"The District Court exceeded its authority today when it barred the President from instructing his cabinet members to enforce existing law," Justice Department spokesman Devin O'Malley said in the statement.
Attorney: Ruling a victory for rule of law
Those fighting the executive order celebrated the decision Monday.
"This is a victory for the American people and the rule of law," San Francisco city attorney Dennis Herrera said in a statement. "This executive order was unconstitutional before the ink on it was even dry.
"We live in a democracy. No one is above the law, including the president."
The statement added that he was "grateful that we've been able to protect billions of dollars that help some of the most vulnerable Americans."
What is a sanctuary city?
The term "sanctuary city" is a broad term applied to jurisdictions that have policies in place designed to limit cooperation with or involvement in federal immigration enforcement actions.
Cities, counties and some states have a range of informal policies as well as actual laws that qualify as "sanctuary" positions.
Most of the policies center around not cooperating with federal law enforcement on immigration policies. Many of the largest cities in the country have forms of such policies.
In 2015, more than 200 state and local jurisdictions did not honor requests from Immigration and Customs Enforcement to detain individuals, ICE Director Sarah Saldaña testified before Congress, and a subset of that group refused to give access to their jails and prisons to ICE.
According to tracking by the Center for Immigration Studies, a think tank that advocates for restricting immigration and opposes sanctuary policies, roughly 300 sanctuary jurisdictions rejected more than 17,000 detention requests, between January 1, 2014 and September 30, 2015.
The idea for sanctuary cities appears to have sprung out of churches in the 1980s that provide sanctuary to Central Americans fleeing violence at home amid reluctance by the federal government to grant them refugee status.
They became popular in more diverse locales to counter what officials there saw as overzealous federal immigration policies, particularly against those arrested for minor, non-violent crimes.
"San Francisco is a sanctuary city and will not waiver in its commitment to protect the rights of all its residents," San Francisco Mayor Edwin Lee said in January, upon announcing an expansion to a city fund to provide legal services to the immigrant community, documented or otherwise.
Chicago has set up a similar fund, as has Los Angeles.
"Chicago has in the past been a sanctuary city," Mayor Rahm Emanuel said in November, according to the Sun-Times. "It always will be a sanctuary city."
Case that sparked immigration debate
The murder trial of an undocumented immigrant has drawn national attention -- in part because he had been deported from the United States five times.
Jose Ines Garcia Zarate's trial
for the shooting of Kate Steinle, on a San Francisco pier in July 2015, stirred the already heated debate over immigration.
The case became a rallying cry for Trump and GOP politicians, who have invoked Steinle's name in decrying sanctuary cities and promoting the construction of a border wall.
Before the shooting, officials in San Francisco had released Garcia Zarate from custody instead of turning him over to immigration authorities.