The policies of sanctuary cities vary but in most cases provide some protections to unauthorized immigrants by not fully cooperating with federal immigration authorities. Trump has repeatedly called for cutting federal funding to these cities, which include New York City, Los Angeles, Washington, Seattle and Boston.
Today's ruling grants a request for a preliminary injunction halting part of an executive order signed by Trump that involved stopping the flow of money to communities that do not cooperate with federal immigration authorities.
The City and County of San Francisco and Santa Clara County filed the lawsuit in question, saying billions of dollars of funding are at risk. However, the Trump administration has said the amount of funding that will be withheld is much lower. The government argued in its response that the suit lacks standing because the order did not change existing law and because the counties that filed it were not named as "sanctuary jurisdictions" in the order.
White House Chief of Staff Reince Priebus responded to the ruling in a briefing with reporters Tuesday night, calling it "clear forum shopping" and "an example of the ninth circuit going bananas." The Trump administration previously criticized the ninth circuit following its decision to uphold a block of Trump's executive order temporarily halting travel from seven countries in the Middle East and Africa.
Priebus added that White House would be taking action to appeal the ruling and expressed confidence in the situation's future, saying, "we'll win at the Supreme Court level at some point."
A subsequent statement from the White House Tuesday night said, "Today, the rule of law suffered another blow, as an unelected judge unilaterally rewrote immigration policy for our nation."
Executive Order 13768, titled "Enhancing the Public Safety in the Interior of the United States," was issued on Jan. 25 and outlines a number of immigration enforcement policies, including one to "[e]nsure that jurisdictions that fail to comply with applicable Federal law do not receive Federal funds, except as mandated by law."
In his decision, U.S. District Judge William Orrick expressed concern at the broadness of the order and sided with the counties, saying the order "by its plain language, attempts to reach all federal grants not merely the three mentioned at the hearing."
The judge called the order "an example of the President's use of the bully pulpit," saying he cannot threaten to withhold funds so broadly.
"And if there was doubt about the scope of the Order, the President and Attorney General have erased it with their public comments," Orrick wrote. "The President has called it 'a weapon' to use against jurisdictions that disagree with his preferred policies of immigration enforcement, and his press secretary has reiterated that the President intends to ensure that 'counties and other institutions that remain sanctuary cities don't get federal funding in compliance with the executive order.'"
The counties have demonstrated that they are currently suffering irreparable harm because the order will deprive them of billions that support core services in their jurisdictions, according to the judge's decision.
"Federal funding that bears no meaningful relationship to immigration enforcement cannot be threatened merely because a jurisdiction chooses an immigration enforcement strategy of which the president disapproves," Orrick wrote.
The Trump administration has said that sanctuary cities allow dangerous criminals back on the street and that the order is needed to keep the country safe. San Francisco and other sanctuary cities have said that turning local police into immigration officers erodes trust with communities, which is to get people to report crimes, The Associated Press reported.
In a press conference Tuesday afternoon, San Francisco City Attorney Dennis Herrera said that city officials have the responsibility to "make sure all of our residents feel that they are a part of the community" and that they can keep the streets "safe."
"This is why we have courts -- to halt the overreach of a president and attorney general who either don't understand the Constitution or choose to ignore it," Herrera said, adding that about $2 billion in funds were at stake in San Francisco alone.
The city would have lost access to funds for medical care, roads and transportation, among other services, Herrera said.
"Because San Francisco stood firm, that's not going to happen here or anywhere else in the United States," the city attorney added.
In a statement, the Department of Justice said, "The Court upheld the 'Government’s ability to use lawful means to enforce existing conditions of federal grants or 8 U.S.C. 1373.' The Department of Justice previously stated to the Court, and reiterates now, that it will follow the law with respect to regulation of sanctuary jurisdictions.
"Accordingly, the Department will continue to enforce existing grant conditions and will continue to enforce 8 U.S.C. 1373. Further, the order does not purport to enjoin the Department’s independent legal authority to enforce the requirements of federal law applicable to communities that violate federal immigration law or federal grant conditions."
The injunction marks the second setback for Trump's immigration agenda. The first iteration of Trump's controversial travel ban -- a major part of his campaign platform -- was blocked in federal court and then withdrawn in favor of the second iteration, which has also been blocked by a federal court.
ABC News' Mike Levine contributed to this report.