WASHINGTON — The Supreme Court on Friday handed President Trump a major victory by clearing the way for him to divert $2.5 billion from the military’s budget and use it to build an extra 100 miles of border wall in California, Arizona and New Mexico.

The justices voted 5 to 4 to lift orders by a federal judge in Oakland and the U.S. 9th Circuit Court of Appeals in San Francisco that had barred the administration from using the Pentagon’s money to build a border wall.

Chief Justice John G. Roberts Jr. and the court’s four other conservatives joined in ruling for the Trump administration. They questioned whether the Sierra Club and other plaintiffs had standing to challenge the government’s spending policy.

The court’s four liberals dissented.

Though the environmental group’s lawsuit challenging the wall will continue in lower courts, Trump can begin using the money for the wall in the meantime.

Trump celebrated the decision on Twitter. “Wow! Big VICTORY on the Wall,” he tweeted.

The decision could allow the president to finally deliver on one of his biggest campaign promises: to erect a southern border wall to block immigrants from illegally crossing into the U.S. The refusal by Congress to allocate wall money has long frustrated Trump, particularly as he approaches the 2020 presidential election.

Despite his claims to the contrary, Trump has not built a single new mile of border barrier since taking office.

American Civil Liberties Union lawyers said they would continue to fight.

“This is not over. We will be asking the federal appeals court to expedite the ongoing appeals proceeding to halt the irreversible and imminent damage from Trump’s border wall,” said ACLU attorney Dror Ladin.

Last month, the chief justice cast the deciding vote to block Trump’s plan to add a citizenship question to the 2020 census. But in the border wall dispute, Roberts cast a decisive vote to clear the way for the wall construction to go forward.

The court issued a one-paragraph statement saying Trump’s appeal had been granted, saying in part: “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 funding law.

Justices Ruth Bader Ginsburg, Sonia Sotomayor and Elena Kagan voted to deny the appeal. Justice Stephen G. Breyer said he would have allowed the government to proceed with contracts, “but not to begin construction” because “that would cause irreparable harm to the environment.”

Trump’s lawyers had asked the high court to intervene, saying the administration faced a Sept. 30 deadline to spend $2.5 billion from the Pentagon’s budget before the fiscal year ended and the money was no longer available.

Lower courts had said Trump’s move to divert the money was an end run around Congress, which had specifically refused to allocate money for a wall.

“The Constitution assigns to Congress the power of the purse,” the 9th Circuit said on July 3 in upholding the injunction issued by U.S. District Judge Haywood Gilliam Jr. “It is Congress that is to make decisions regarding how to spend taxpayer dollars.”

That was a reference to the deadlock between the president and Congress over the border wall. It led to a 35-day partial government shutdown, which ended in February with a budget deal that included just $1.4 billion for border security, but nothing for a wall. It was well short of the $5.7 billion Trump had sought for a wall.

After signing the deal, the president declared a national emergency and said he had the authority to transfer already appropriated funds to extend the border wall.

Lawyers for the Sierra Club, the Southern Border Communities Coalition and the ACLU sued, arguing that construction of a 30-foot-high wall would harm wildlife and damage the environment in remote areas.

In the last two years, U.S. Solicitor Gen. Noel Francisco has repeatedly gone directly to the Supreme Court to challenge orders issued by district judges in fast-moving cases. Normally, the high court chooses to review cases only after they have gone through lower courts.

But Francisco asked the justices again to get involved, noting the growing number of nationwide injunctions that have blocked executive proclamations or regulations issued by the Trump administration.

His appeal in Trump vs. Sierra Club contended the border wall was needed to combat drug smuggling. The Sierra Club’s “interest in hiking, birdwatching and fishing in designated drug-smuggling corridors do not outweigh the harm to the public from halting the government’s efforts to construct barriers to staunch the flow of illegal narcotics across the southern border,” Francisco said.

According to the Defense Department, he said, the disputed funds “will no longer remain available for obligation after the fiscal year ends on Sept. 30.”

In response, the Sierra Club said the court should not let the administration “swiftly spend billions of dollars that Congress denied.” To do so would show “the shutdown was essentially a charade,” the lawyers said.

The Sierra Club had told the court that the border wall construction could damage “protected public lands such as Organ Pipe National Monument, Coronado National Memorial, the Cabeza Prieta National Wildlife Refuge and the San Bernardino National Wildlife Refuge.”