ALBANY—The Republican-led Senate in New York voted to legalize same-sex marriage on Friday evening, delivering to gay-rights advocates a hard-fought victory that they hope will tip the balance in the country toward their cause.

The culmination of a tense week of anguished deliberations and fervent demonstrations, the Senate approved gay marriage with a vote of 33 to 29, an outcome that hinged on the emergence at the very last hour of two additional Republican votes.

The bill had already cleared the Democrat-led Assembly and has the enthusiastic backing of Gov. Andrew Cuomo, a first-term Democrat who has made gay marriage a priority during his first months in office.

Mr. Cuomo signed the bill into law Friday night, making New York the sixth and largest state in which gays and lesbians can legally marry. The Senate vote also marks the first time in the nation that a legislative body controlled by Republicans has voted to adopt gay marriage.

"I know my vote is a vote of conscience, and I am certainly at peace with my vote," said Stephen Saland, a 67-year-old attorney from Poughkeepsie, disclosing his stance to the public just moments before the votes were recorded. Joining Mr. Saland was Sen. Mark Grisanti of Buffalo, who became the fourth Republican behind the bill, which also carried the support of 29 Democrats.

Gov. Cuomo reacts legislation legalizing same-sex marriage was passed by the state Senate. Associated Press

People celebrate inside of Stonewall Inn in NYC after the New York Senate's vote. Reuters

Proponents of gay marriage scored their biggest victory since their setback in 2008, when California voters supported a ban. They hope the vote will give them a surge of momentum at a time when other states, including Oregon, Maine, Washington and Maryland, are weighing the same issue.

The overwhelming majority of the Senate Republican conference opposed gay marriage, with many expressing fear of alienating Catholic voters and other conservative and religious constituencies that make up their base. Others argued that the easier path would be to finally put to rest one of the most contentious issues ever debated in Albany.

More New Yorkers React as Gay Marriage Law Passes

Without embracing the bill, some Republicans pushed hard for more legal protections for religious groups that object to gay marriage. Mr. Cuomo negotiated final changes to the bill, which broadened the exemptions to insulate religious charities from legal liabilities and restrict state and local government from imposing sanctions. Another unusual provision specified that if any of the exemptions were struck down or removed, the entire law would be invalidated.

"I believe if this bill fails, I believe that the next time around those religious protections won't be there," said Mr. Grisanti on Friday evening.

Catholic leaders in New York said they were appalled by the vote. "Governor Cuomo has opened a new front in the culture wars that are tearing at the fabric of our nation," said Bishop Nicholas DiMarzio of Brooklyn in a statement. He said he has asked Catholic schools to refuse any distinction or honor bestowed by the governor or lawmakers who voted for the bill and to ban those legislators from speaking at any parish or school celebration.

Opponents of gay marriage pray after the Senate vote Associated Press

Ultimately, Republican concerns were outweighed by pressure from Mr. Cuomo, who lent his muscle to a disciplined, multimillion dollar advocacy campaign waged by national and local gay-rights leaders.

At the present, gay marriage is legal in Massachusetts, Connecticut, Vermont, New Hampshire and Iowa, as well as Washington, D.C. Adding on New York, those places would constitute more than 10% of the American population. Several other states allow for civil unions of same-sex couples.

Nationally, 53% of Americans think marriages between same-sex couples should be recognized by the law as valid, according to a Gallup poll from May. That figure is closer to 60% among New York voters, according to recent polls.

President Barack Obama has said his position on the issue is evolving but still stands by a federal law that defines marriage as a legal union between a man and a woman.

"The president is lagging behind a majority of Americans," said Evan Wolfson, the founder of Freedom to Marry, a national gay-rights group. "At this point, he'd hardly be breaking new ground. He would be standing squarely in the center of American hearts and politics on the freedom to marry."

It was the second time the bill has come to the floor of the New York's Senate, which in 2009 rejected gay marriage by a decisive margin when the chamber was controlled by Democrats. Not a single Republican voted for the bill then, and eight Democrats also voted no.

The lobbying against the measure was more scattered, but at times intense. New York's archbishop, Timothy Dolan, railed against Albany for rushing to "tamper with a definition as old as human reason." At Mass on Sunday, the bishop of Brooklyn assailed by name a Brooklyn Democratic senator for endorsing gay marriage.

Supporters of gay marriage celebrate at the Capitol in Albany. Associated Press

Advocates credit their victory to the clout of a popular governor and to show of unity of a movement that at times had fallen victim to clashes of ego and tactics.

Since their loss in 2009, gay activists raised money to help unseat three lawmakers opposed to gay marriage and replace them with senators who pledged their support for the bill. And when Republicans took back the chamber last year, advocates recruited a team of veteran Republican strategists close to the conference and drew financial support from wealthy Republican businessmen allied with the movement.

Calling the issue a "fundamental civil-rights battle," Mr. Cuomo leaned heavily on lawmakers to take up the measure and coordinated closely with the advocacy campaign.

"This would not have happened if Governor Cuomo weren't the driving force," said Michael Long, chairman of the Conservative Party. "He is, no doubt, the driving force."

For many Republicans, their largest fear was drawing the ire of Mr. Long, a key power-broker for the conference, who threatened to abandon his support for any Republican who voted for the bill.

But a pivotal factor was a less obvious break in strategy, which for years had been anchored by the assumption that New York would never legalize gay marriage as long as Republicans controlled the Senate.

Instead of trying to defeat Senate Republicans and prop up their adversaries, gay-rights activists wooed the them with a blend of courtship and threats that gradually wore down their resistance to a bill and opened the door for a winning vote.

The new plan was spearheaded by the Gill Action Fund, a powerful gay-rights group led by Tim Gill, a libertarian-leaning philanthropist from Denver. Mr. Gill's team of operatives and network of donors helped turn the tide on gay marriage in other statehouses, including Iowa and New Hampshire.

Last year, Mr. Gill's group pumped in nearly $1 million into a political action committee called Fight Back NY, which financed attack ads against three vulnerable senators—Democrats Hiram Monserrate and Bill Stachowski and Republican Frank Padavan—who voted against the bill in 2009. They lost their seats to gay-marriage friendly candidates.

"We wanted to send a very strong and clear message about what happens when you double-cross us," said Mr. Gill's political director, Bill Smith, an Alabama-born political operative who began his career in the 1990s working under the tutelage of Karl Rove.

Mr. Smith then hired a trio of old Albany hands who had long-standing and close relationships with the Senate Republicans. Michael Avella, a former chief general counsel for the Senate GOP, did the lobbying, feeding intelligence to a field operation led by the Human Rights Campaign.

John McArdle, former Senate leader Joe Bruno's top communications director, advised on messaging and strategy. And for polling, Smith hired the same pollster whom Senate Republicans had used for years, Claude LaVigna. Advocates figured that Republicans would be more likely to trust his survey results, which showed that while most Republicans opposed gay marriage, fewer of them cared enough about it to make it a decisive factor at the ballot box.

Mr. Smith also raised more than $1 million from Republican National Committee chairman Ken Mehlman and hedge-fund managers Paul Singer and Daniel Loeb, and other Wall Street figures. That money was crucial to the gay-rights coalition in New York, financing more than half of this year's television and print campaign promoting gay marriage.