Congress gave final approval early today to a compromise fiscal 1987 budget of nearly $1 trillion that slashes President Reagan's request for defense but holds out the prospect of more military funding if the president agrees to raise taxes.

The budget accord was approved by a conference committee and then ratified by 333 to 43 in the House and by voice vote in the Senate with four members present, paving the way for Congress to leave town for a two-week Fourth of July recess.

Approval of the compromise followed a month of maneuvering over defense and taxes, three days of intensive, closed-door talks by senior budget committee leaders from the two houses and a full day of haggling over final domestic spending details, including a Medicare dispute resolved by rollback of scheduled increases in patient costs for hospitalization.

Praise for the budget was muted. House Budget Committee Chairman William H. Gray III (D-Pa.) said the resolution represented "in light of political reality . . . a credible approach."

Senate Budget Committee Chairman Pete V. Domenici (R-N.M.) said it was "better to produce this budget resolution than not to have one at all."

White House officials in Santa Barbara, where the president is vacationing, said the administration is likely to accept the budget compromise, despite the defense cutbacks.

The proposed budget defies Reagan by anticipating more spending for domestic programs and less for defense than he called for in his own budget request for next fiscal year, although it stops short of mandating tax increases that Reagan has adamantly opposed and threatened to veto.

His $320 billion defense spending request was cut $28 billion; foreign aid was cut 10 percent below current levels and even more below Reagan's request; most domestic programs were frozen or trimmed rather than cut as drastically as the president proposed; virtually all of his proposals for program terminations were ignored.

But administration officials said the White House wanted to avoid a battle over the budget now because it could interfere with prospects for Hill approval of tax overhaul legislation and because some ground could be regained, officials believed, in the appropriations process.

As a guide for congressional tax and spending decisions, the budget resolution need not be signed by the president, although he has veto power over subsequent legislation, such as appropriations bills, that result from budgetary allocations.

At least on paper, the fiscal 1987 budget pact meets the $144 billion deficit target set by last year's Gramm-Rudman-Hollings budget-control law, which requires automatic, across-the-board spending cuts if the target is exceeded by $10 billion or more.

According to preliminary estimates, the deficit specified in the budget is $142.6 billion for next year, declining to $78 billion within three years. But the budget includes assumed savings that may not be achieved, and economic assumptions that would produce a lower deficit than is now anticipated, raising the possibility of triggering the Gramm-Rudman-Hollings sanctions even if the budget specifies a deficit that is within the ceiling.

"I don't know if we've given ourselves a wholly adequate cushion," said House Majority Leader James C. Wright Jr. (D-Tex.) in otherwise endorsing the budget as a "true compromise" worthy of approval.

Congress is more than two months late in approving a budget resolution under deadlines set by Gramm-Rudman-Hollings, leaving little time to pass legislation needed to carry out its objectives, including appropriations and spending cutback ("reconciliation") legislation to implement about $40 billion in deficit reductions. When Congress returns from recess July 14, it will have only a month before the initial deficit-estimating "snapshot" is taken, indicating whether the new sanctions are to be invoked.

Procedures for triggering the automatic cuts are under legal attack at the Supreme Court, with a ruling expected soon. But the law contains a fallback provision requiring Congress to make the cuts if the court knocks out the trigger, posing lawmakers with an election-year choice between imposing painful spending cuts and ignoring the law.

The budget calls for new defense spending authority of $292.1 billion, considerably short of the amount estimated necessary to keep pace with inflation. This is in sharp contrast with earlier budgets that gave the Defense Department hefty above-inflation increases that added up to a near-doubling of the defense budget in the last six years.

The budget would allow an increase in current defense appropriations of about $5 billion but would chop nearly $28 billion from Reagan's $320 billion request for next fiscal year, which envisioned an above-inflation increase of about 8 percent. In their separate versions of the budget resolution approved earlier in the year, the House agreed to $285 billion for defense, the Senate $301 billion.

The compromise sets up a contingency fund of $4.8 billion for next year that would be available to the president for "unmet critical needs" if Reagan requests and Congress approves taxes, user fees, asset sales or spending cuts to offset the additional cost.

Two-thirds of the additional outlays could go for defense, which would drive the spending authority figure up to $299 billion, enough to cover the cost of inflation but not much more.

Although tax increases are not specified in the agreement because of House Democratic queasiness over backing any increases opposed by Reagan, it is considered unlikely that Congress would go along with any of the alternatives. The budget already includes user fees and asset sales, and spending cuts are so sensitive that the House-Senate conference almost bogged down at one critical point over cuts amounting to less than 1 percent of the $70 billion Medicare budget.

The tax-contingency compromise was a partial victory for all sides. Reagan avoided a direct challenge from Congress on taxes, but the Senate succeeded in nudging the White House on taxes for defense and the House held to its position that the president had to lead the way on taxes before it would go along.

The budget assumes a full cost-of-living increase for federal retirees as well as Social Security recipients; federal retirees' pensions were frozen last year. It also allows for a 3 percent pay raise for military and civilian employes of the federal government in each of the next three fiscal years.

While providing for continuation of nearly all 44 programs that Reagan wanted to terminate, the budget froze or cut most of them. For instance, the Urban Development Action Grant program (UDAG), Economic Development Administration and Appalachian Regional Commission would be continued but cut 10 percent. Mass transit would be cut 10 percent; Amtrak funding would be increased.