Senate approval was the final hurdle for the same-sex marriage legislation, which was approved last week by the Assembly. Gov. Andrew M. Cuomo signed the measure at 11:55 p.m., and the law will go into effect in 30 days, meaning that same-sex couples could begin marrying in New York by late July.

Passage of same-sex marriage here 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 currently permit same-sex marriage: Connecticut, Iowa, Massachusetts, New Hampshire and Vermont, as well as the District of Columbia.

At around 10:30 p.m., moments after the vote was announced, Mr. Cuomo strode onto the Senate floor to wave at cheering supporters who had crowded into the galleries to watch. Trailed by two of his daughters, the governor greeted lawmakers, and paused to single out those Republicans who had defied the majority of their party to support the marriage bill.

“How do you feel?” he asked Senator James S. Alesi, a suburban Rochester Republican who voted against the measure in 2009 and was the first to break party ranks this year. “Feels good, doesn’t it?”