The Democrats counted their gains from Capitol Hill to the county courthouses yesterday, after a midterm election that left President Reagan confronting a choice between compromise and stalemate on his basic program.

Reagan said Republicans have "every reason to feel good" about retaining the eight-vote U.S. Senate majority that they won on his coattails in 1980. But that was about their only consolation prize on a day that saw the Democrats considerably strengthen their hand Tuesday by gaining 26 House seats, seven governorships and at least nine state legislative houses.

Although the Democrats failed to gain in the number of seats they hold in the Senate, they won 20 of the 33 contested on Tuesday and "came within a whisper," in the words of Democratic Senatorial Campaign Committee chairman Wendell H. Ford (Ky.), of wresting back control of that chamber.

A shift of just 43,000 votes in five states -- Nevada, Rhode Island, Vermont, Missouri and Wyoming -- would have given the Democrats those seats and Senate control.

"So much for Republican realignment dreams," said Democratic National Chairman Charles T. Manatt, savoring a victory that will magnify his party's voice in policy decisions for the next two years and improve its prospects for the 1984 presidential race.

While Reagan insisted publicly that he was "very pleased" with the outcome, House Speaker Thomas P. (Tip) O'Neill Jr. (D-Mass.) declared it "a disastrous defeat for the president."

Some White House officials argued that at least on certain issues, such as the preservation of the scheduled third year of the tax cut in 1983, there were enough Republicans left in the House to continue the conservative coalition with southern Democrats that gave Reagan his string of legislative victories in 1981 and early 1982.

But recognizing that most of the newly elected Democrats are moderates or liberals and many Republicans who survived the upsurge of party-line Democratic voting on Tuesday were nervous about automatic support of the president, Republican congressional leaders echoed the Democrats' urging for a new era of bipartisan compromise.

"There's no question there will have to be some alterations" in Reagan's program, said House Minority Leader Robert H. Michel (R-Ill.), who struggled to his toughest-ever victory in recession-wracked Peoria. "We're obviously not in a position to call the tune as we have for the last two years."

Senate Majority Leader Howard H. Baker Jr. (R-Tenn.) said his "greatest fear" is that without compromise, "we might end up with a year or so of stalemate."

Reagan said he was always ready for "concessions and compromises," but gave no hint that he would back off the planned defense increases or domestic budget cuts that many winning Democrats criticized in their campaigns.

While reshuffling the legislative deck in Washington, voters also signaled their support for nuclear weapons control by approving "freeze" referenda in eight states.

But the issues that dominated the election were unemployment, Social Security and the economy -- all of them working for the Democrats.

The election that ended Reagan's almost unbroken domination of Capitol Hill was marked by an apparent increase in voter turnout -- reversing a 20-year trend--and by a show of precinct power by the Democrats and their labor and liberal allies that had not been seen in years.

The party push was frustrated in the Senate battles, where Democrats could manage no more than a stalemate. They won Republican seats in New Jersey and New Mexico, but saw the Republicans escape with less than 52 percent of the vote in six other states. Republican pickups in Nevada and Virginia offset the Democrats' gains.

But the Democrats' organization effort -- drawing on the resources of labor, black, Hispanic, elderly, feminist, consumer, disarmament and environmental groups, sharply critical of Reagan administration policies -- showed powerfully in the House contests and even more effectively in state elections.

"We proved the Democratic coalition is alive and well," said Ann Lewis, political director of the Democratic National Committee.

Democrats overcame the Republicans' financial advantages and defeated 26 GOP House incumbents, more than half of them members of the 1980 freshman class who regarded themselves as shock troops of the "Reagan revolution."

The losers included some of Reagan's earliest allies in the House, such as Rep. Thomas B. Evans (R-Del.), a three-termer, and some of the loudest critics of O'Neill's reign, such as freshman Rep. John LeBoutillier (R-N.Y.).

Many of the Democratic gains came in industrial centers in the hard-hit Midwest, including Toledo, Ohio; Pontiac, Mich.; and Rock Island, Ill. But they also came through strongly in the Sun Belt, scoring multiple-seat gains in Florida, California, Virginia, Texas, and North Carolina.

"It really was a national victory," said Lewis, noting that the Democrats had spread their gains through 15 states and all four regions.

The North Carolina victory was a personal triumph for Gov. James B. Hunt Jr. (D), who led the party's forces to two upset victories over candidates backed by Sen. Jesse Helms (R-N.C.) in what was widely seen as a trial run for their expected confrontation in 1984, when Helms' term expires.

All in all, it was a bad day for the New Right forces allied with Helms, whose Congressional Club saw 15 of its 18 endorsed candidates go down. Sixteen of the 17 senators targeted by the National Conservative Political Action Committee (NCPAC) -- all but Nevada Democrat Howard W. Cannon -- survived.

But the real damage to the Republican Party was suffered, not on its right flank, but in the heartland of the country -- and the party -- where moderate-to-conservative governors and gubernatorial candidates were mowed down en masse.

The Democrats won nine governorships: Alaska, Arkansas, Michigan, Minnesota, Nebraska, Nevada, Ohio, Texas and Wisconsin. Only two -- California and New Hampshire -- flipped the other way. The Illinois battle between Republican Gov. James R. Thompson and former senator Adlai E. Stevenson III showed Thompson with a 1 percentage point lead, but counting problems in Chicago made the outcome uncertain.

Retirements that opened the way to Democratic takeovers in Alaska, Michigan, Minnesota, Ohio, and Wisconsin also stripped the party of such moderate leaders as Govs. Jay S. Hammond, William G. Milliken, Albert H. Quie, James A. Rhodes and Lee Sherman Dreyfus. The seat of a sixth such retiring moderate, Iowa Gov. Robert D. Ray, was held for the Republicans by conservative Lt. Gov. Terry Branstad.

Reagan tried, but failed, to save conservative Govs. Robert F. List of Nevada and Charles Thone of Nebraska with late campaign trips. But the loss that hurt worst was the defeat of Texas Gov. William P. Clements Jr., who went all out to help Reagan carry his state in 1980.

Not only does that mean that a Democrat, state Attorney General Mark White, will be in the governor's chair when the Republicans hold their national nominating convention in Dallas in 1984, but it also caps a frustrating record for the GOP in the whole band of southern states where Reagan ran so well in 1980 against President Carter, a southerner.

The Democrats reclaimed the governorships of Texas and Arkansas (where ex-governor Bill Clinton settled his grudge with Gov. Frank D. White), they put George C. Wallace back in the Alabama governor's office occupied for the last four years by a Democrat-for-Reagan, Fob James. All told, in the South the Democrats won seven of eight governorships and four of five Senate seats, most by wide margins. They also gained eight House seats, while the Republicans lost two.

Those gains reflected not just party strength, but also reapportionment gains carved out by Democratic legislatures (with help from the courts) to frustrate Republican dreams of easy victories in new Sun Belt seats.

The same pattern showed up in Reagan's West, where Democrats elected five of the nine senators and eight of the 10 governors. They picked up eight House seats, while Republicans gained only one.

But in the West the GOP salvaged a consolation prize by recapturing the governorship that Democrat Edmund G. (Jerry) Brown Jr. has held for eight years since Reagan relinquished it. Winning by a narrow margin, state Attorney General George Deukmejian (R) ended the hopes of Los Angeles Mayor Tom Bradley (D) to become the first elected black governor in American history. Brown lost to San Diego Mayor Pete Wilson (R) in the race for the U.S. Senate.

While organized labor lost both those races, it helped one of its staunchest allies, liberal Democrat Phillip Burton, save his threatened House seat in San Francisco.

Overall, union political chiefs said, Tuesday was one of the best days they have had in years. The victory of 24 of their 33 endorsed gubernatorial candidates was the best since the "Watergate" year of 1974. Twenty of the 31 labor-endorsed Senate candidates won.

Women fared less well in the elections. The three female Senate candidates were defeated: New Jersey Republican Rep. Millicent Fenwick, Missouri Democratic state Sen. Harriett Woods, who went into the voting at least even-money bets to win, and New York Republican Florence Sullivan.

Women gubernatorial candidates lost in Iowa and Vermont, but the first woman treasurer was elected in Ohio. And in Florida, site of one of the major Equal Rights Amendment battles, the number of women state senators jumped from four to nine.

Blacks added three seats in the U.S. House of Representatives, picking up districts in Kansas City, New York City, and Gary, Ind. But state Rep. Robert G. Clark (D) failed in his bid to become the first black elected to the House from Mississippi.

While pollsters and politicians differed somewhat in their interpretations of Tuesday's vote, most agreed that it signaled the end of one phase of the Reagan presidency and the start of another.

Automatic majorities in the House of Representatives are a thing of the past. The GOP's seat loss was more than double the post-World War II average for newly elected presidents.

Lobbyists and congressional aides endorsed the view that was expressed by a National Association of Manufacturers' spokesman, who said Reagan's leadership "has been weakened by the election."

Even the Democrats conceded, however, that Reagan has formidable weapons to employ in the coming legislative battles. He is credited by many with turning the tide in the Nevada Senate race that cost the Democrats their only incumbent defeat.

Republicans assert -- though some Democrats disagree -- that his "stay the course" plea held down the damage to Republican candidates.

"I'd hate to think what this election would have been if he had gone off to Europe for the last three weeks," said one Republican pollster.

But the loss of the governorships weakens the GOP for the 1984 presidential election, as Republicans acknowledged. They had set as their minimum goal holding four of the eight largest states. As things stand, they come away from Tuesday only with California, Pennsylvania and--perhaps--Illinois.

Senate control beyond 1984 is also in jeopardy. The failure by Republicans to make any net gain in a year when there were 20 Democratic seats on the ballot and only 13 of their own leaves them vulnerable for 1984. In that year, they must defend 19 seats and the Democrats only 14.

In the meantime, Democrats were looking forward to a new order.

"No longer," said Democratic Congressional Campaign Committee chairman Tony Coelho (Calif.), "will Reagan be able to run a partisan government. He will have to run a coalition government."