North Carolina’s Republican-dominated legislature upheld a bill Tuesday night that will ban most abortions after 12 weeks, overriding the Democratic governor’s recent veto of the new restrictions.

The success of the override was a victory for Republicans and a critical test of their new, but slim, supermajority. The vote, taken in both chambers in back-to-back sessions, means a dramatic change for abortion access in North Carolina, where abortion is currently legal up to 20 weeks. The vote also restricts access for women across the South, some of whom have traveled to North Carolina for abortions from states where the procedure is largely banned.

The new law is set to take effect July 1.

The vote came just days after Gov. Roy Cooper vetoed the bill at a raucous rally near the legislative building in Raleigh, and after a public campaign to pressure Republican lawmakers to vote against the ban.

Given the Republican supermajority in both chambers of the state legislature, the override was not entirely a surprise. But it was also not assured. Just one vote could have tipped the outcome.

The Senate voted in favor of the override, and the House took up the bill about an hour later. Immediately after the House had voted to approve the ban, around 8:30 p.m., crowds of onlookers in the chamber’s gallery chanted: “Shame! Shame! Shame!”

In a statement minutes after the override vote’s conclusion, House Speaker Tim Moore said, “I am proud that the House has overridden the governor’s veto of this meaningful, mainstream legislation.”

The ban, known as Senate Bill 20, would restrict most abortions in North Carolina to 12 weeks, with exceptions for rape, incest, certain fetal abnormalities and the life of the mother. The ban also mandates that detailed information about abortion procedures be reported to state health regulators and institutes longer waiting periods and more in-person medical visits to obtain an abortion.

“North Carolinians now understand that Republicans are unified in their assault on women’s reproductive freedom, and we are energized to fight back on this and other critical issues facing our state,” Mr. Cooper said after the override. “I will continue doing everything I can to protect abortion access in North Carolina, because women’s lives depend on it.”

North Carolina’s 12-week ban is less restrictive than other state laws that prohibit the procedure beginning at conception or after six weeks of pregnancy. Republicans said the bill was a compromise that would protect innocent life and support women, but abortion-rights activists said it would prevent women across the South from gaining access to abortion and would put lives at risk.

The override was a huge blow to Mr. Cooper and his political allies, who had asked North Carolina voters to contact their legislators and urge them not to override the veto, zeroing in on four Republicans from districts with substantial numbers of Democratic constituents in the hope that he could persuade to vote against the party line.

One of those lawmakers was Tricia Cotham, a longtime Democrat who, in a surprise move, switched parties last month, handing Republicans supermajorities in both chambers of the state legislature. Ms. Cotham had earlier this year backed a bill that would have legalized abortion until viability, considered about 22 or 23 weeks. But in May, she voted in favor of the 12-week ban, and she did so again during the override vote on Tuesday night.

“I believe this bill strikes a reasonable balance on the abortion issue and represents a middle ground that anyone not holding one of the two extremist positions can support,” Ms Cotham said in a statement after the vote.

Republicans have struggled to find consensus on abortion bans since the Supreme Court last year kicked regulation back to the states when it overturned Roe v. Wade. In South Carolina, the G.O.P. has been unable to reach agreement and pass a new abortion ban for months because of objections from within the party. Nebraska is considering a 12-week ban after its attempt to pass a six-week ban failed.

Abortion opponents have pushed for stricter bans, but they celebrated the North Carolina override as a victory.

“The battleground state of North Carolina has taken a major step forward in the fight for life,” said Marjorie Dannenfelser, president of Susan B. Anthony Pro-Life America.

Republican strategists said the ban could prove to be an important litmus test of the political realities for Republicans, especially in purple states like North Carolina.

“Legislatively, they’re on offense, but politically, they’re on defense, which is a bizarre place to be,” said Doug Heye, a former Republican National Committee communications director and a North Carolina native who has consulted on three Republican U.S. Senate campaigns in the state. “They’re feeling this issue out for what’s acceptable to the broader public.”

Dr. Kristin Baker, a House Republican, was the final legislator to speak before the vote. The bill “balances protecting the life of the unborn child,” she said, over boos from the gallery, adding: “It balances that with a woman’s need for life saving care. And, importantly, it protects the integrity of the doctor-patient relationship.”

On Monday, entities that do business throughout North Carolina, including the online-rating company Yelp, the British manufacturer Lush Cosmetics and the upscale Raleigh eatery Death & Taxes, signed a petition opposing the bill, arguing that it could damage North Carolina’s standing as a magnet for business.

Restricting access to services including abortion “will not only deter future businesses from investing in our great state; it will also jeopardize the trust of those companies who have already established roots here,” said Ashley Christensen, a chef and the proprietor of a half-dozen restaurants and food-service businesses in the Raleigh area, who signed on to the petition.

Though Mr. Cooper and other Democrats vowed to keep fighting for abortion rights, it is unclear what maneuvers they might have left. During about two combined hours of debates, Democrats in the House and the Senate were by turns indignant and tearful about the bill before them.

“It is honestly hard for me to believe that my government would do this to me, to my daughters, to my friends, to their daughters,” said Natasha Marcus, a Senate Democrat from the Charlotte area.