“One of the things I said I have to do and I want to do is border security, because we have tremendous amounts of drugs flowing into our country, much of it coming from the southern border,” he said when he announced his emergency declaration. “We’re going to confront the national security crisis on our southern border, and we’re going to do it one way or the other.”

White House officials said in a memo that the administration would redirect $601 million from the Treasury’s forfeiture fund, up to $2.5 billion from Department of Defense funds allocated to counter drug activity, and up to $3.6 billion from Department of Defense capital projects.

But the president’s critics objected that such action was not allowed by law. Congress, they said, was granted control over the country’s finances by the Constitution.

Mr. Trump’s remarks at times undercut his own emergency declaration. “I didn’t need to do this, but I’d rather do it much faster,” Mr. Trump said. “I just want to get it done faster, that’s all.”

Within days, California and 15 other states joined together to sue the administration and prevent it from moving the funds; four other states have since joined the lawsuit.

The states requested a temporary injunction against the administration while the entire case is heard. Days later, the American Civil Liberties Union also filed a lawsuit against the president on behalf of the Sierra Club and the Southern Border Communities Coalition to block the president from accessing the Department of Defense funds. They argued that the president’s emergency declaration was both an abuse of power and that it violated the National Environmental Policy Act.

“Our lawsuit seeks to stop the president’s illegal power grab before it causes irreversible harm to border communities and the rule of law,” said Dror Ladin, a staff attorney for the ACLU, who argued the case.

“The case is not about whether the challenged border barrier construction plan is wise or unwise. It is not about whether the plan is the right or wrong policy response to existing conditions at the southern border of the United States,” Judge Gilliam said in the order. “Instead, this case presents strict legal questions regarding whether the proposed plan for funding border barrier construction exceeds the Executive Branch’s lawful authority.”