As the Senate debated the measure, supporters and opponents from around the state packed into two small galleries overlooking the chamber. When the final vote tally was read, the crowd screamed and hollered, began to chant “U.S.A.! U.S.A.!” — and to yell “thank you.”  A minute or two later, when Gov. Andrew M. Cuomo entered the chamber, the crowd cheered again, rushing the edge of the galleries and chanting the governor’s name.
Senate approval was the final hurdle for the legislation, which was strongly supported by Mr. Cuomo. The Assembly approved changes made by the Senate, after passing an earlier version last week. Mr. Cuomo signed the measure late Friday, and the law will go into effect in 30 days, meaning that same-sex couples could begin marrying in New York by midsummer. “I am very proud of New York and the statement we made to the nation today,” Mr. Cuomo said.
The bill’s passage followed a daunting run of defeats in other states where voters barred same-sex marriage by legislative action, constitutional amendment or referendum. Just five states — Connecticut, Iowa, Massachusetts, New Hampshire and Vermont — permit same-sex marriage. It is also legal in : the District of Columbia.
The approval of same-sex marriage represented a reversal of fortune for gay-rights advocates in New York State, who just two years ago suffered a humiliating and unexpected defeat when a same-sex marriage bill was easily defeated in the Senate, which was then controlled by Democrats. This year, with the Senate controlled by Republicans, the odds against passage of same-sex marriage appeared long.
But the unexpected victory had an unlikely champion: Mr. Cuomo, a Democrat who pledged last year to support same-sex marriage but whose early months in office were dominated by intense battles with lawmakers and some labor unions over spending cuts.
Mr. Cuomo made same-sex marriage one of his top priorities for the year and deployed his top aide to coordinate the efforts of a half-dozen local gay-rights organizations whose feuding and disorganization had in part been blamed for the defeat two years ago. .
The new coalition of same-sex marriage supporters brought in one of Mr. Cuomo’s trusted campaign operatives to supervise a $3 million television and radio campaign aimed at persuading a handful of Republican and Democratic senators to drop their opposition.
For Senate Republicans, even bringing the measure to the floor was a freighted decision. Most of the Republicans firmly oppose same-sex marriage on moral grounds, and many of them also had political concerns, fearing that allowing same-sex marriage to pass on their watch would embitter conservative voters and cost the Republicans their one-seat majority in the Senate.
Leaders of the state’s Conservative Party — whose support many Republican lawmakers depend on to win election — warned that they would oppose in legislative elections next year any Republican senator who voted for same-sex marriage.
But after days of agonized discussion capped by a marathon nine-hour closed-door debate on Friday, Republicans came to a fateful decision: the full Senate would be allowed to vote on the bill, the majority leader, Dean G. Skelos, said Friday afternoon, and each member would be left to vote according to his or her conscience.
"The days of just bottling up things, and using these as excuses not to have votes — as far as I’m concerned as leader, its over with," Mr. Skelos, a Long Island Republican, said.
Twenty-nine Democrats voted for the measure, joined by four Republicans: James S. Alesi of Monroe County; Stephen M.. Saland, from the Hudson Valley area; Roy J. McDonald of the capital region; and Mark J. Grisanti of Buffalo.
Just one lawmaker rose to speak against the measure: Rubén Díaz, Sr. of the Bronx, the only Democratic senator to cast a no vote.
“God, not Albany, has settled the definition of marriage, a long time ago,” Mr. Diaz said.