The Senate, responding to President Reagan's dramatic uphill lobbying drive, gave him a resounding foreign policy victory yesterday when it voted, 52 to 48, against adopting a motion that would have vetoed his $8.5 billion aircraft sale to Saudi Arabia.

With four senators who previously had been opponents of the sale shifting their support to the president at the last moment, the final vote saw Reagan winning by an unexpectedly comfortable margin on an issue that he contended was of vital importance to his Mideast goals and his ability to assert effective leadership in world affairs.

The president immediately expressed his gratitude by describing the Senate's decision as "the upper chamber at its best" and reaffirming that America's "unshakable commitment" to Israel will be unchanged. He said the vote will strengthen U.S.-Saudi relations, protect "our economic lifeline" to Saudi oil and aid progress toward peace in the Middle East.

"I think it's going to have a very good effect," Reagan added in a prepared statement. "We had heard from many leaders who had expressed their concern for what this would mean on the world scene."

Among the four whose votes gave Reagan the victory was Sen. William S. Cohen (R-Maine), whose father is Jewish and who himself is a Unitarian. He provided the most electrifying moments in the day-long debate preceding the vote with an impassioned speech in which he charged the Saudis with being immoderate foes of Israel and argued that Reagan's actions have "left the Israelis confused and frightened by our policies."

But, Cohen continued, Reagan's decision to sell the Saudis Airborne Warning and Control System (AWACS) radar planes and other equipment had put Israel in "a no-win situation" that raised the danger of the Jewish state becoming a scapegoat if the package were defeated. For that reason, Cohen said, he regarded Reagan's pledge to maintain Israel's military superiority over its Arab foes as grounds for regarding approval of the sale as the lesser evil, and he added he would support the sale on that basis.

In addition to Cohen, the other senators who crossed to the president's side at the last minute were Republicans Slade Gorton of Washington and Mark Andrews of North Dakota and Democrat Edward Zorinsky of Nebraska. All were among the original 49 cosponsors of a resolution offered by Sen. Bob Packwood (R-Ore.) to disapprove the sale.

The House voted 301 to 111 to adopt the disapproval resolution on Oct. 14. If the Senate had followed suit yesterday, the Saudi sale, the largest arms deal in history, would have been blocked. It also would have marked the first instance of Congress exercising its right to veto a major arms sale.

During most of the protracted controversy over the AWACS sale, it appeared that such a veto would occur. As recently as last weekend, the numbers, as expressed in the public positions of various senators, showed a majority against the sale, although backers of the president insisted that they had more support than was publicly evident.

It was not until Tuesday that the intensive personal lobbying blitz mounted by Reagan and Senate Majority Leader Howard H. Baker Jr. (R-Tenn.) showed signs that the tide had turned in the administration's favor.

On Tuesday, Reagan gained the support of nine previously uncommitted senators and won the conversion of a previously outspoken foe, Roger W. Jepsen (R-Iowa). It also became known then that Gorton and Andrews were preparing to switch. The total effect of these changes was to ensure that when the debate began yesterday the opposition would be one vote short of the 51 needed to adopt the resolution, if the full Senate voted.

Then, as the debate seesawed, Cohen announced his switch. Although there were rumors about Zorinsky, he did not speak in the debate, and his change of heart was not made clear until the roll call. The Nebraska senator, who is Jewish, changed after a last-minute visit to the White House where Reagan reportedly told him: "How can I convince foreign leaders that I am in command if I cannot sell five airplanes?"

At a news conference after the vote, Sen. Alan Cranston (D-Calif.), one of the opposition leaders, said he had expected the Gorton and Andrews defections and added that Cohen's switch was "the clincher." Cranston added that he did not know until the last minute how Russell B. Long (D-La.), the only undeclared senator, would vote.

Before Cohen and Zorinsky went over to the president, Long had appeared to be the opposition's only hope of getting the 51st vote needed to adopt the resolution. But by the time the roll was called, Long's vote no longer mattered; in the end he voted for the sale as had been expected.

Because it was clear from the outset yesterday that only something totally unexpected could prevent a Reagan victory, the debate was largely a lifeless affair invigorated only by a few flashes of drama such as Cohen's speech. Still, the participants, who had fought so hard on both sides of the issue, dutifully went through the motions of making their arguments one last time.

The opposition's case was summarized in such phrases as Packwood's contention that "arming the Middle East is going to produce neither peace nor oil"; the argument of Claiborne Pell (D-R.I.) that the issue was "not a question of what is most beneficial to the United States, but what will be less harmful," and Cranston's assertion that "a rubber-stamp Senate may be what the president wants, but it's not what he needs nor what the nation needs."

On the president's side, Sen. Charles H. Percy (R-Ill.), chairman of the Foreign Relations Committee, said rejection of the sale "would pose the most serious threat to Israel's security" because it would damage Reagan's ability to play a mediating role in the Arab-Israeli conflict. Baker asserted that the administration "went to extraordinary and difficult lengths to hear every senator" with views and concerns about the deal, and Sen. Charles McC. Mathias Jr. (R-Md.) said: "I am persuaded that the legitimate concerns . . . have been answered."

Several senators--including Gorton, who cited it as a major reason for his switch--said they originally had great reservations about the dangers to Israel or other possible misuse of the equipment and had changed their minds only after Reagan promised to send the Senate a letter spelling out the safeguards built into the deal.

The White House had kept the letter in incomplete form as a device for overcoming the specific objections of Gorton, S. I. Hayakawa (R-Calif.) and Frank H. Murkowski (R-Alaska) by including language acceptable to them. The final draft of the letter sent to the Senate yesterday put in writing points previously made by administration spokesmen. In the letter, Reagan promised that none of the five AWACS planes would be delivered to Saudi Arabia until the conditions contained in the letter have been met.

These cover matters relating to security of the aircraft, the geographic limits of where they will be operated, the inclusion of U.S. training personnel in the AWACS crews, the sharing of data collected by the planes and the seeking of Saudi cooperation in the peace process.

In the debate, opponents pointed out repeatedly that none of these safeguards is binding on the Saudis, but the president's partisans replied that they were adequate to meet all concerns about misuse of the equipment.

The initial reaction to the vote from American Jewish leaders, who had opposed the sale strongly, was restrained. Howard M. Squadron, chairman of the Conference of Presidents of Major American Jewish Organizations, raised several questions about future Mideast policy but added the hope that the sale "will result in a strengthening of our country's position in the Middle East."

Rabbi Alexander M. Schindler, president of the Union of American Hebrew Congregations, said: "Democracy has spoken, and all Americans will now accept the decision of the Congress and work to restore that unity essential to the well-being of our society."

In addition to the five AWACS planes, the sale includes aerial refueling tankers, AIM9L Sidewinder air-to-air missiles and range-enhancing fuel tanks for the 62 F15s the United States agreed to sell to the Saudis in 1978.