In Spearfish, S.D., a waste water treatment lagoon required and approved by the Environmental Protection Agency dissolved underlying gypsum deposits and became a subterranean river of sewage that poisoned wells and surfaced at nearby cattle ranches.

Three small communities at Mountain Top, Pa., have teetered on the brink of bankruptcy since they completed their $10 million sewer plant, and were unable to build collector pipes to get much of the sewage to the plant.

When EPA officials cleared out of Ely, Minn., after building an experimental waste water treatment plant, they left the town's 5,000 residents to pick up annual operating costs equal to nearly half the community's total budget of $1 million.

In the last 10 years, the federal government has spent $30 billion to eliminate pollution from the nation's lakes and rivers. Through the EPA, it has employed both the stick and the carrot.

The stick is the threat of criminal penalties and the loss of funds for communities that discharge pollutions from their sewer systems. The carrot is the government's offer to pay 75 percent of the costs of highly sophisticated treatment plants.

One result: whatever the improvement in water quality, the EPA grants program has left a legacy of bitterness and fiscal turmoil in communities victimized by its rigid administration.

In some cases, the federal apparatus imposed on small communities sewage treatment requirements that are completely out of touch with reality.

As a result, during last year's presidential campaign, Ronald Reagan struck a chord that was already reverberating in a thousand town halls across the country.

His promise to get the government off the backs of taxpayers and local officials seemed to be the long-prayed-for salvation from frustrations like those produced by the EPA grants program.

It is not clear, however, that the Reagan administration will be any less rigid or any more effective, even if the program is stripped of its funding or turned over to the states.

As the Reagan era begins, cities and counties everywhere are shouldering the financial burden of costly waste water treatment plants, many poorly desgined, needlessly complex and incapable of removing the promised degree of pollutants.

The communities' operating and maintenance costs for fuel, electricity and chemicals have grown faster than the federal grants for plant construction. a

In the end, millions of Americans have been unpleasantly surprised by soaring sewage bills. taxpayers are hit several times: first to support the federal grants program with tax funds, second to help pay their local share and third to pay the operating costs through sewerage fees.

Some cities appear to accept the hardships as the price of modernization. Others are fighting back.

At Mountain Top, local officials have taken construction contractors and their bonding company to court for walking away from unfinished work. Even though federal funds are at stake, the EPA has remained above the fight.

In Ely, local officials expressed outrage at the costly, energy-gobbling systems that EPA designers approved for the $2.6 million advanced waste treatment plant. To appease them, the EPA agreed to subsidize part of the plant's operating expenses until a solution can be found.

In many cities, citizens have banded together to fight, often in federal court, their local governments and the EPA. In Marshall Township, Pa., a group of neighbors has organized to protest a $4 million EPA sewer project whose major benefit will not go to the community's 220 families but to a 1,00-acre industrial park.

In Skagway, Alaska, town officials have hired the Pacific Legal Foundation to represent them in their fight to make the EPA take back its treatment plant, which the city claims is subject to flooding, too costly to operate and not needed to protect any body of water.

Skagway's legal champion is known for its ties to White House counselor Edwin Meese III, who was on its advistory board, and Interior Secretary James G. Watt, who worked for a sister foundation.

The EPA has retaliated by taking Skagway officials to court, seeking $10,000 a day in civil penalties and threatening the mayor with criminal penalties.

A group of South Dakota ranchers is suing the engineers, contractors and government officials who built the lagoon at Spearfish because it poisoned wells and fouled property.

"It was just a fiasco from the word go," said Elizabeth Swift, the town's finance director. She said town officials have been reluctant to challenge EPA edicts.

The ranchers' lawyer, Jospeh V. Karaganis, explained: "Out in western South Dakota, the land is loaded with gypsum. Gypsum is a rock, but when it's exposed to water, it's just like sugar in coffee.

"Along came an engineering outfit in the early '70s to build a treatment plant for Spearfish, and they said, 'We're going to build a lagoon.'"

Despite the ranchers' warning that the sewage would get into the underground water supply, the EPA approved the engineers' plan and the lagoon was built.

"From the day the lagoon opened, it was leaking," the lawyer said. "They had put a clay liner under it, but clay, like any other material if you take the support out from under it, will go 'blooey.'"

When the gypsum dissolved, the lagoon did just that.

"An underground cavern opened up," Karagains said, "and dropped the support out from under the lagoon, whereupon all of the sewage in the lagoon drained out and showed up a mile and a half away on my clients' property."

Local officials around Moosehead Lake, near the central Maine town of Greenville, tried to tell EPA officials that the area didn't need a multimillion-dollar, high-technology sewer plant to handle a few hundred houses on the lake's southern shore.

Town leaders referred a simpler, cheaper system that would spray waste water as fertilizer on farmland. But the EPA insisted that the lake, which showed no signs of pollution, must be protected.

Local officials relented after the EPA offered a $3 million grant to finance the bulk of the project. It was completed in 1976, but never accepted. Even the engineers who designed it could not get it to work.

In addition, by the time it was completed, estimates of operating costs had leaped from $28,000 a year to $125,000. In a town where the average income was about $9,000 a year, sewer bills would have jumped from almost nothing to as high as $400 a year had the plant been commissioned. It remains idle, a monument to the lawsuits that have followed.

As it is, the sewer district has defaulted on its local share of funding for the project, and is five years behind in payments to the Farmers' Home Administration on a $527,000 loan.

When they went back to the drawing board, the EPA and Maine agreed to spend additional millions to build what the Greenville leaders asked for in the first place: a system that sprays waste water on the land, where they are now happily growing Christmas trees.

Thus, in six years, the federal government and the state of Maine have spent $9 million in a town of fewer than 2,000 residents for sewage treatment facilities that have not in any way improved the water quality of neighboring Moosehead Lake.

As a National Science Foundation report asked about a similar circumstance in another city. "Would it not have been more effective, in terms of national water pollution control, to spend the funds in communities with more serious problems?"

The lesson of Greenville, according to Hubbard Trefts, chairman of the Moosehead Sewer District, is that "There was a completely inadequate analysis of the community's ability to repay [the local costs of the plant] and the community's ability to manage a super-sophisticated system in a very isolated area. If anything had gone wrong, we would have had to call an expert 200 to 300 miles away to fix it."

In Suffolk County, N.Y., residents have shouldered a swelling property tax burden to help defray an expected $1 billion in additional costs on the Southwest Sewer District Project.

Begun in 1969, the project was the largest municipal undertaking ever in that Long Island county, and was designed to protect the Glacial Aquifer, a body of pure water underlying parts of Suffolk.

As the urban sprawl of New York City brought more and more residents to the county during the 1960s, scientists began to fear that cesspools and septic tanks could pollute the drinking water drawn from underground.

Plans were drawn for a system that would serve a 57-square-mile area and 300,000 residents. About 900 miles of sewer lines would be laid. A $30 million treatment plant would be constructed, and a giant sewer discharge pipe would be laid 2 1/2 miles out into the atlantic Ocean.

For all this, county officials estimated a cost of $269 million.

Today, after cost overruns, interest charges, alleged illegal payments and just plain miscalculations, the price tag is about $1.5 billion, according to a congressional report. County officials say the final figure will be closer to $1 billion.

The $30 million treatment plant has become a $70 million plant. The $12 million pipe into the ocean has become a $50 million pipe. Interest charges are estimated at $400 million to $900 million over the life of the project. And by 1976 hundreds of miles of sewer pipes had sprung nearly 4,000 leaks, according to the Suffolk Water Authority.

In 1979, the company that manufactured much of the concrete pipe was convicted, along with one of its owners, of defrauding the EPA.

Two county politicians and the chief partner of the consulting engineering firm that designed the sewer project face trial on felony charges that they lied to a grand jury when they denied receiving part of nearly $1 million in payoffs related to the project.

The investigation into sewer corruption in Suffolk County has cost taxpayers an additional $500,000. In January, the district attorney filed for damages totaling $270 million alleging a "pattern of racketeering" by former county officials, the consulting engineer, several construction firms and the concrete pipe manufacturer.

Earlier this month, a New York grand jury returned indictments alleging that the county's principal engineering firm, Bowe, Walsh & Associates, bribed public officials and political figures to get contracts.

The Long Island newspaper Newsday reported in 1979 that two of the largest contractors on the sewer project had rescued the Bahamian land venture of a prominent Suffolk Republican official, Nicholas Barbato, and his partners.

Barbato had played a leading role in overseeing the sewer project. The contractors who rescued the land deal had been awarded $200 million worth of construction work by the county.

The EPA, which has committed more than $200 million in federal tax funds to the project, did not make an inspection of construction activity on Long Island until four years after it began.