Amid warnings that it was embarking on a "march of folly," Congress last night passed and sent to President Reagan historic legislation to force steady reductions in the federal deficit over the next five years and adoption of a balanced budget by fiscal 1991.

The Senate, after a nine-hour debate, approved the measure by a vote of 61 to 31. The House then quickly followed suit, voting 271 to 154 to pass the legislation.

The White House, in a statement shortly after the final vote, said Reagan would sign the measure "first thing" this morning.

Although a narrow majority of House Democrats opposed the bill and Senate Democrats split evenly on it, the measure gained enough bipartisan support for enactment, a reflection of how action on the skyrocketing deficit has become a political imperative in Congress. It was approved despite widespread fears, voiced even by some supporters, that the measure could cause "chaos" in the federal budget beginning next year.

"It could be disastrous," said Rep. Richard A. Gephardt (D-Mo.) in urging passage of the bill. "But the question is not if this is good policy. The question is can you let this [deficit] madness go on."

The balanced-budget bill -- whose chief sponsors are Sens. Phil Gramm (R-Tex.), Warren B. Rudman (R-N.H.) and Ernest F. Hollings (D-S.C.) -- is an amendment to legislation lifting the national debt ceiling to more than $2 trillion.

With passage of the package and the promise of the president's signature, there was no danger of a government default even though Reagan will sign the legislation after the midnight expiration of a temporary debt-ceiling extension, which was passed last month. White House spokeswoman Denny Brisley said that because of the president's assurance, "The Treasury Department and the Federal Reserve will be able to continue normal financial operations without interruption."

Reagan endorsed the bill Tuesday night shortly after a House-Senate conference committee cleared a final version after weeks of negotiations. The president also reiterated his intention to continue increasing military spending without raising taxes, setting up a potentially explosive confrontation with Congress next year over budget priorities.

Before the final Senate vote yesterday, Finance Committee Chairman Bob Packwood (R-Ore.) predicted that Reagan's next budget, for fiscal 1987, will call for elimination of 30 to 50 domestic programs in order to meet the deficit target of $144 billion set in the balanced-budget bill.

If Congress and the president failed to meet the target, a series of automatic, across-the-board budget cuts would be imposed on most government programs, including defense spending.

"Make no mistake about it -- this bill is a historic watershed," Packwood said. He added that Congress should "pray that what we are about to undertake will work."

But critics of the legislation charged that it will not work and that if the automatic cuts are triggered they will cripple many domestic programs and endanger national defense.

"Gramm-Rudman is a suicide pact," said Sen. Daniel Patrick Moynihan (D-N.Y.). "We are entering into an agreement with the administration to dismantle the defenses of the United States."

Sen. J. Bennett Johnston (D-La.) called the legislation "a freight train bearing down on every American, not just the Pentagon."

"It is the silliest thing if it weren't so tragic for the country," he added.

Sen. Gary Hart (D-Colo.), who characterized the measure as a "march of folly," said the size of the deficit is the result of Reagan's economic policies, which he called "a disaster for this nation's economy."

"We are here today to pay the bill for an economics that has failed," Hart said.

But defenders of measure, including Gramm, Packwood and Senate Budget Committee Chairman Pete V. Domenici (R-N.M.), argued that the mandatory features of the bill offered the only hope of gaining control of the ballooning deficit, which is estimated at about $200 billion for the current fiscal year. It will, they asserted, force the president and Congress to make the difficult decisions necessary to reduce the deficit.

"Could it be any worse than what we've got now?" Domenici asked. "I kind of concluded no."

Gramm said, "This, in fact, completes the Reagan [economic] program. What this bill does is force decisions. It does not dictate."

Senate Republicans were joined by a number of liberal Democrats in supporting the legislation. Arguing that the deficit poses a growing threat to social programs, Sen. Edward M. Kennedy (D-Mass.) accused Reagan of using the deficit "as the excuse for a wholesale assault" on federal programs, whether they are working or not. "Now a procedure is in place to ensure that Congress will address the deficit responsibly," Kennedy added.

In the Senate, 22 Democrats and 39 Republicans voted for the bill, while 22 Democrats and nine Republicans opposed it. In the House, the measure was supported by 118 Democrats and 153 Republicans, and opposed by 130 Democrats and 24 Republicans.

Assailing the bill during the 90-minute House debate, Rep. David R. Obey (D-Wis.) said, "The trouble with Gramm-Rudman is that Gramm-Rudman is a lie." He said it will not lead to deficit reduction but will produce "massive chaos" and will "damage the economy in a thousand ways."

"It is an act of desperation," said Rep. E. Clay Shaw Jr. (R-Fla.), "but now is the time to act even if it is in desperation. We are drowning ourselves in red ink."

Under the legislation, mandatory and declining targets for the deficit are set for the next five years, ending with a balanced budget in fiscal 1991, which begins Oct. 1, 1990. The automatic spending cuts will be imposed to reach the targets if Congress exceeds the deficit limits in any year.

The bill exempts Social Security, several antipoverty programs and interest on the national debt from mandatory cuts, and it provides the president with limited flexibility in fiscal 1986 to allocate spending reductions among categories within the defense budget.

But the measure calls for the automatic cuts to come half from defense spending and half from domestic programs, clearly heightening concern among some members of Congress that it will seriously damage the nation's military strength.

Meanwhile, Office of Management and Budget James C. Miller III submitted to Reagan yesterday a package of $50 billion in domestic budget cuts for fiscal 1987 that would be required to meet the $144 billion deficit target in the balanced-budget legislation.

If accepted by the president, such a package would be one of the largest sought by the administration. Officials said Miller presented the package as a single recommendation and that it included many cuts proposed by Reagan last year, but not adopted, in programs such as the Small Business Administration and Amtrak.

Reductions and outright terminations were also expected to be proposed in such domestic programs as student aid and assistance to states and cities, officials said.