“I urge my colleagues to stand up for the rule of law and defeat this bill,” Senator Russ Feingold, Democrat of Wisconsin, said Wednesday as the outcome was all but assured.

The final plan, which overhauls the Foreign Intelligence Surveillance Act passed by Congress in 1978 in the wake of Watergate, reflected both political reality and legal practicality, supporters said.

Wiretapping orders approved by secret orders under the previous version of the surveillance law were set to begin expiring in August unless Congress acted. Heading into their political convention in Denver next month and on to the November Congressional elections, many Democrats were wary of handing the Republicans a potent political weapon.

The issue put Senator Barack Obama, the presumptive Democratic presidential nominee, in a particularly precarious spot. He had long opposed giving legal immunity to the phone companies that took part in the N.S.A.’s wiretapping program, even threatening a filibuster during his run for the nomination. But on Wednesday, he ended up voting for what he called “an improved but imperfect bill” after backing a failed attempt earlier in the day to strip the immunity provision from the bill through an amendment.

Mr. Obama’s decision last month to reverse course angered some ardent supporters, who organized an Internet drive to influence his vote. And his position came to symbolize the continuing difficulties that Democrats have faced in striking a position on national security issues even against a weakened president. Senator Hillary Rodham Clinton, Democrat of New York, who had battled Mr. Obama for the nomination, voted against the bill.

Senator John McCain, the likely Republican presidential nominee, was campaigning in Ohio and did not vote, though he has consistently supported the immunity plan.

Support from key Democrats ensured passage of the measure.

Senator John D. Rockefeller IV, the West Virginia Democrat who leads the intelligence committee and helped broker the deal, said modernizing the Foreign Intelligence Surveillance Act was essential to give intelligence officials the technology tools they need to deter another attack. But he said the plan “was made even more complicated by the president’s decision, in the aftermath of Sept. 11, 2001, to go outside of FISA rather than work with Congress to fix it.”