U.S. District Judge William Orrick issued the preliminary injunction in two lawsuits - one brought by the city of San Francisco, the other by Santa Clara County - against an executive order targeting communities that protect immigrants from deportation.
The injunction will stay in place while the lawsuits work their way through court.
The judge said that President Donald Trump cannot set new conditions for the federal grants at stake. And even if he could, the conditions would have to be clearly related to the funds at issue and not coercive, Orrick said.
"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," the judge said.
A Justice Department attorney, Chad Readler, had defended the president's executive order as an attempt to use his "bully pulpit' to "encourage communities and states to comply with the law."
The Trump administration had further argued the lawsuits were premature because the government hasn't cut off any money yet or declared any communities to be sanctuary cities.
Meanwhile, mayors from several U.S. cities threatened with the loss of federal grants emerged from a meeting Tuesday with Attorney General Jeff Sessions saying they remain confused about how to prove their police are in compliance with immigration policies - a necessary step for them to receive grant money.
During a recent court hearing, the Trump administration and the two California governments disagreed over the order's scope.
San Francisco and Santa Clara County argued that the order threatened billions of dollars in federal funding for each of them, making it difficult to plan their budgets.
But Readler, acting assistant attorney general, said the threatened cutoff applies to three Justice Department and Homeland Security grants and would affect less than $1 million for Santa Clara County and possibly no money for San Francisco.
In his ruling, Orrick sided with San Francisco and Santa Clara, saying the order "by its plain language, attempts to reach all federal grants, not merely the three mentioned at the hearing."
"And if there was doubt about the scope of the order, the president and attorney general have erased it with their public comments," the judge said.
The Trump administration says 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 say turning local police into immigration officers erodes trust that is needed to get people to report crime.
The order also has led to lawsuits by Seattle; two Massachusetts cities, Lawrence and Chelsea; and a third San Francisco Bay Area government, the city of Richmond. The San Francisco and Santa Clara County lawsuits were the first to get a hearing before a judge.
San Francisco and the county argued that the president did not have the authority to set conditions on the allocation of federal funds and could not compel local officials to enforce federal immigration law.
The sanctuary city order was among a flurry of immigration measures Trump has signed since taking office in January, including a ban on travelers from seven Muslim-majority countries and a directive calling for a wall on the Mexican border.
A federal appeals court blocked the travel ban. The administration then revised it, but the new version also is stalled in court.
Send a breaking news alert
Report a correction or typo
Learn more about the 6abc apps