WASHINGTON — The Supreme Court has allowed President Trump to defy Congress and continue to spend more than $6 billion diverted from military funds to pay for the construction of a border wall in parts of Arizona, New Mexico, Texas and California.

By 5-4 vote, the justices on Friday rebuffed lawyers for the Sierra Club and House Democrats who sued to challenge Trump’s diversion of funds as illegal and unconstitutional.

They won rulings before judges in California and Texas, but in a brief order last summer, the court allowed Trump to continue spending the disputed funds while the litigation continues.

But over the dissents of the four liberal justices, who said the decision would likely “operate in effect as a final judgment,” the court kept that order in place.

Last week, lawyers for the ACLU and the Sierra Club filed an emergency appeal arguing that the court’s order would permit the border wall projects to be completed before the high court ever gets a chance to rule on whether the spending was legal. House Democrats filed a brief in support of the appeal.

Keeping the stay in place offers Trump “a complete victory without having prevailed in any court,” they told the justices.

The case offers a stark example of how the president can defy the Constitution and its separation of powers with an assistance from the high court.

Early last year, Trump demanded $5.7 billion for the border wall, but the House of Representatives, under Democratic control, refused, triggering a partial government shutdown that lasted 35 days. The impasse ended when Trump signed a new spending bill that did not include the funding he sought for the border wall.

But a day later, he declared a national emergency and ordered the Pentagon to transfer $2.5 billion to pay for projects involving border walls. The administration said the new barriers, extending up to 130 miles, were designed to prevent “drug smuggling.” Later, Trump ordered the transfer of another $3.6 billion for new border barriers in Texas.

The Constitution says, “No Money shall be drawn from the Treasury but in Consequence of Appropriations made by law.”

In Trump’s defense, administration lawyers cited a provision in law that authorized the transfer of military funds in emergencies. However, as lower-court judges pointed out, the law says these transfers must be “based on unforeseen military requirements ... and in no case where the item for which funds are requested has been denied by the Congress.”

They said Trump’s transfers failed on both counts. The need to stop drug smuggling or immigrants from crossing the border was not “unforeseen,” and Congress did indeed deny the requested funds for the border wall.

But the high court has handcuffed Congress and others who seek to block the executive branch from taking allegedly illegal actions. Lawmakers, as well as ordinary citizens, do not have standing to sue to challenge unconstitutional spending.

The Sierra Club sued on behalf of environmentalists and Native Americans who claimed the border wall would damage or destroy sensitive habitats near the border. They were granted standing in the lower courts, but that alone was not enough to obtain a court ruling, at least according to the justices.

Last year, the five more conservative justices issued a one-line, unsigned explanation of their decision to give Trump a green light to proceed despite the lower courts’ orders.

“Among the reasons is that the Government has made a sufficient showing at this stage that the plaintiffs have no cause of action to obtain review of the Acting Secretary’s compliance with” the provision that authorizes the transfer of military funds, they said by a 5-4 vote in Trump vs. Sierra Club.

This suggests that because Congress did not specifically authorize lawsuits over this part of the military spending law, no one may go to court to contest an allegedly illegal transfer.

The result is to shield the president and his administration from legal claims that he is diverting government funds to be spent for other purposes.

After the stay was granted last summer, the case went back before U.S. District Judge Haywood Gilliam in Oakland. He ruled the transfers were illegal, and he issued another order that would bar further spending of the military funds for building border walls. The 9th Court of Appeals agreed June 26, in a 2-1 decision.

This set the stage for the administration to file a formal appeal with the Supreme Court seeking review of the ruling. But that would take months and would not yield a ruling until sometime in the first half of next year.

That prospect of delay prompted last week’s effort to have the justices lift the stay and block further construction of the wall.

“This case concerns a protracted and public appropriations debate, irreparable damage to a protected landscape, substantial separation of powers concerns, and the diversion of billions of taxpayer dollars to a project that Congress refused to fund,” attorneys for the Sierra Club and the American Civil Liberties Union told the court. But their appeal was denied.

Dror Ladin, a staff attorney with the ACLU’s National Security Project, said opponents of the wall would not give up the legal battle.

“Every lower court to consider the question has ruled President Trump’s border wall illegal, and the Supreme Court’s temporary order does not decide the case,” he said. “The administration has admitted that the wall can be taken down if we ultimately prevail, and we will hold them to their word and seek the removal of every mile of unlawful wall built.”