“People of America and people of the world can breathe a sigh of relief,” said Senator Chuck Schumer of New York, the Democratic leader.
In anticipation of Republican defections, President Trump sent Vice President Mike Pence to the Senate floor to break a tie vote. But with three members of his own party breaking away, Mr. Pence could do nothing.
“We were surprised and thrilled to win on this,” said Tiernan Sittenfeld, senior vice president of the League of Conservation Voters, which, along with other environmental groups, has been lobbying Republicans for weeks to vote against the repeal of the methane rule. “This is clearly a huge win for our health and our climate.”
While Ms. Collins and Mr. Graham had publicly announced their opposition to the measure, Mr. McCain’s vote was a surprise.
It came as Senator Mitch McConnell of Kentucky, the majority leader, was advancing the nomination of the trade lawyer Robert Lighthizer to be the United States trade representative. Mr. Lighthizer harshly criticized Mr. McCain during the senator’s 2008 presidential campaign, saying his free-trade views were not in the best traditions of American conservatism. Mr. McCain has since sought to block Mr. Lighthizer’s nomination, objecting to a waiver granted to him to skirt the president’s prohibition on lobbyists serving in his cabinet.
Mr. McCain also was the target of significant lobbying on the climate issue. Last week, a group of retired generals from the liberal Vet Voice Foundation sent a letter to all senators, framing the rollback of the methane regulation as a national security threat. The group said the venting of the emissions from fuel wells could diminish the natural gas supply. Mr. McCain, who is chairman of the Senate Armed Services Committee, has spoken of climate change and the energy supply as a national security issue.
Oil lobbyists pressed their case that the methane rule was onerous and duplicative, since many oil-producing states already have state-level methane regulations.
On Tuesday, Gwen Lachelt, a county commissioner from La Plata County, Colo., which sits near the Four Corners where the state abuts New Mexico, Utah and Arizona, buttonholed Mr. McCain in a Senate elevator to tell him that county residents have suffered from methane pollution drifting over from New Mexico, and she noted that the same pollution could affect his state.
“I’m not taking credit for swaying Senator McCain’s vote, but I told him that right across the state line from my county are 35,000 oil and gas wells in New Mexico,” she said. “We all share an airshed and the winds that bring methane pollution our way, and without this federal rule, I have no way as a county commissioner to protect the people in my county. In the Four Corners, we all live under the largest methane cloud.”
Mr. McCain made a similar point in a statement on the reasons for his vote.
“Improving the control of methane emissions is an important public health and air quality issue, which is why some states are moving forward with their own regulations requiring greater investment in recapture technology,” he wrote. “I join the call for strong action to reduce pollution from venting, flaring and leaks associated with oil and gas production operations on public and Indian land.”
Nevertheless, Mr. McCain did say he would support an effort by the Trump administration to rewrite the Obama rule. Had Congress repealed the Obama-era rule entirely, the administration would have been precluded from issuing any similar regulation under the terms of the Congressional Review Act.
The Independent Petroleum Association of America, which represents smaller oil and gas producers, criticized the vote. “We’re disappointed the Senate wasn’t able to stop President Obama’s unworkable rule by a federal agency that does not have the congressionally granted authority to regulate air quality,” said Barry Russell, the group’s president.
Oil-state Republican lawmakers vowed to push Interior Secretary Ryan Zinke to act on his own.
“I call on Interior Secretary Zinke to withdraw the rule immediately,” said Senator John Barrasso of Wyoming, who is chairman of the Senate Committee on Environment and Public Works. “If left in place, this regulation will only discourage energy production, job creation and economic opportunity across the West.”
The failure of the effort to roll back the methane rule is a small but significant victory for environmentalists in the Trump era, as the president has pushed aggressively to dismantle most of his predecessor’s environmental legacy.
The methane rule was one of a suite of environmental regulations put in place by Mr. Obama as he sought to use his executive authority to tackle climate change across the economy. While methane vented from oil and gas wells accounts for only a small portion of the nation’s greenhouse gas pollution, environmental advocates urged Mr. Obama to tackle the emissions, because methane is over 25 times more potent than carbon dioxide in trapping heat in the earth’s atmosphere.
The failure to repeal the methane rule, which required a simple majority of 51 votes to pass, comes as the Senate nears the end of its window to use the Congressional Review Act to undo major regulations. Under that law, which was used successfully only once before this year, Congress can vote to erase new executive-branch regulations within 60 legislative days of their completion. Since Mr. Trump’s inauguration, the White House has successfully pushed Congress to roll back more than a dozen Obama-era rules.
Included in those rollbacks were regulations that would have limited the way coal mining companies could dump debris into streams after blowing up mountaintops to gain access to coal deposits, and a requirement for oil, gas and mining companies to disclose payments made to foreign governments in exchange for access to drilling or mining rights.
An earlier version of this story misstated a figure in a quotation from Gwen Lachelt, a county commissioner in Colorado. She said there were 35,000 oil and gas wells across the state line in New Mexico, not 35.