Gov. Jerry Brown’s bid to keep California’s cap-and-trade system — a key weapon against global warming — running through 2030 won the approval Monday night of state legislators, by the narrowest of margins.

Despite fierce opposition from both the left and the right, Brown’s bill won approval in the senate and assembly, with barely a vote to spare. The approvals ensured that a climate program Brown has touted as a model for other states and countries will continue past 2020.

“Tonight, California stood tall and once again, boldly confronted the existential threat of our time,” Brown said in an emailed statement Monday night. “Republicans and Democrats set aside their differences, came together and took courageous action. That’s what good government looks like.”

The bill, AB 398, had been the subject of an intense week of lobbying, by the governor and his opponents. Republicans complained the bill would drive up prices for gasoline and electricity, while some environmentalists slammed it as being far too kind to industry.

Passage required two-thirds of the legislators in each house. Twenty-eight senators voted in favor — one more than needed. Surviving the assembly required 54 votes in favor, and the bill secured 55. Several assembly Republicans crossed party lines to support it, even though some noted that California represents just 1 percent of the world’s greenhouse gas emissions.

“You’re right, we’re a very small component of the world on this,” said Assemblyman Rocky Chavez, R-Oceanside (San Diego County). “But that doesn’t mean we shouldn’t be leaders on something that’s threatening the world.”

Both houses also approved a companion measure, AB 617, that will increase monitoring of industrial air pollution and toughen penalties for polluters. And they approved a related measure pitched by Republicans, ACA 1, that would put before California voters a ballot measure requiring a one-time, two-thirds vote of the Legislature to appropriate money raised by the cap-and-trade system.

Cap and trade sets an annual limit on California’s greenhouse gas emissions and forces companies to buy a permit, called an allowance, for every metric ton of gas they emit. The number of allowances available each year matches the annual limit, and both decrease slowly over time, lowering emissions.

MBA BY THE BAY: See how an MBA could change your life with SFGATE's interactive directory of Bay Area programs.

California law requires the state to cut its greenhouse gas emissions to 40 percent below 1990 levels by 2030. And yet, the state Legislative Analyst’s Office had questioned whether the state’s cap-and-trade system, launched in 2012, had legal authorization to continue running past 2020.

Opponents of cap and trade had for years tried to convince courts that the system constituted an illegal tax on businesses, since it was never approved by the two-thirds legislative vote required for new taxes. So Brown was determined that any extension pass by a two-thirds vote.

Democrats, who control both chambers, cast the bill as vital to California’s leadership on global warming policy, even though many of their own environmental allies criticized its details.

“At a point in time when the president of the United States has withdrawn us from the Paris climate agreement and is doing everything in his power to eliminate American leadership on climate, it is more important than ever for California to send a crystal-clear message to the rest of the world,” said Sen. Scott Wiener, D-San Francisco.

Republican senators hammered the cap-and-trade extension as a thinly veiled tax that would hit poor Californians hardest.

They cited a letter from the Legislative Analyst’s Office in March that estimated cap and trade could raise gasoline prices anywhere from 24 to 73 cents per gallon in 2031. That letter, however, did not analyze AB 398, whose language was only unveiled last week and which contains measures to cap price impacts.

“We could shut down the entire state of California, and it would have absolutely no effect on the global climate,” said Republican Sen. Andy Vidak from the Central Valley. “But what is measurable is the effect this tax will have on the poorest of the poor in my district and throughout California.”

First used in the 1990s to control sulfur dioxide and acid rain, cap and trade gives companies flexibility in how, where and when they cut emissions. As a result, it has often been touted as a more business-friendly way to fight pollution than imposing strict limits on emissions from individual facilities.

That flexibility, however, has fed suspicion of cap and trade among some environmentalists, who say it gives companies the option of paying to pollute. Instead, they want tighter control of emissions from individual factories and refineries, in the hopes that forcing each facility to cut greenhouse gases will reduce other, harmful air pollutants as well.

Brown’s bill would block that approach. It prohibits both state and local air pollution regulators from slapping hard limits on greenhouse gas emissions from any facilities already covered by cap and trade.

The move — a bid to win support from the oil industry and its legislative allies — infuriated environmental justice advocates, who waged a week-long lobbying campaign to kill the bill. The Bay Area Air Quality Management District, whose directors had been poised to vote on greenhouse gas limits for refineries, complained that Brown had undercut its authority.

The bill’s backers readily acknowledged the compromises involved.

To win approval, Brown and legislative leaders tossed in measures sought by industry — and by Republicans. For example, the bill would suspend until 2031 the $152 annual fire-prevention fee imposed on many rural homeowners, replacing the lost funding with money raised through cap and trade. Republican representatives from rural districts had long complained about the fee.

David R. Baker is a San Francisco Chronicle staff writer. Email: dbaker@sfchronicle.com Twitter:@DavidBakerSF