A West German court today sentenced the three surviving leaders of the notorious Baader-Meinhof urban guerrilla gang to life imprisonment, bringing to a close the most spectacular, most controversial and probably most important trial in West Germany's postwar history.

The three defendants -- Andreas Baader, 33: Gudrun Ensslin, 36: and Jan-Karl Raspe, 32 -- were found guilty of murder in the deaths of four U.S. soldiers in terrorists bomb attacks against U.S. installations in Frankfurt and Heidelberg in May 1972.

In announcing the verdict on behalf of a five-judge panel, presiding Judge Eberhard Foth also found the defendants guilty of 34 attempted murders --most involving the wounding of other GIs in those same attacks -- and guilty of belonging to a criminal organization pledged to do harm to the state.

Although the verdict was expected, security forces were placed on alert around the country against possible retaliation by extremist sympathizers.

The importance of the Baader-Meinhof case has grown far beyond the fate of the individual defendants. The case has produced a barrage of tough new laws and proposed laws for dealing with terrorists. It has also led to the use of controversial police methods and has had an important impact on differing public attitudes about how far police and legal authorities should go in protecting the public against acts of terrorism.

The case has come to symbolize the dilemma of dealing with terrorists in a democracy, a question that is especially acute in Germany because of both a particularly virulent contemporary breed of terrorist and a history in which anarchy once led to Hitler.

On the one hand, there is strong support for tough law-and-order codes. On the other, there are critics who argue that West Germany is damaging its own legal system and civil rights with superfluous legislation that could become harmful.

The three who were sentenced today had assumed political responsibility for their actions as leaders of the group known as the Red Army Faction. But they have not admitted carrying out the attacks themselves.

Their lawyers have contended throughout the 23-month trial that the accused anarchists are not criminals but rather ideologically motivated guerrillas who "were helping the liberation struggle of the Vietnamese people against an imperialist power, America, with West Germany as its accomplice."

Leading defense lawyer Otto Schily insisted that as a result, the defendants should be tried as prisoners of war.

Foth, however, described this as murder "under the alibi of the antiimperialist struggle."

Three weeks ago, motorcycle-riding terrorist shot and killed West Germany's chief public prosecutor, Siegfried Buback, who had been a central figure in pressing the case against the three defendants.A group calling itself the Ulrike Meinhof Action Group has claimed responsibility.

At Buback's funeral, West German Chancellor Helmut Schmidt cautioned against excesses in reaction to the killing, warning that "emotionally charged, indiscriminate, uncontrolled reactions" play into the hands of anarchists "so they can then denounce our country as a fascist dictatorship."

Originally, there were five imprisoned leaders of the Baader-Meinhof gang. One of them, Holger Meins, died in jail in November 1974, after a hunger strike protesting prison conditions. The next day a West Berlin judge was killed in retaliation.

Last May the group's ideological leader, Ulrike Meinhof, was found hanged in her cell in what an official inquiry has labeled suicide.

Today's verdict was handed down in Stuttgart, in the fortress like court-room that was specially built for this case next to the maximum-security prison where the defendants have been held.

The defendants and their defense counsels were absent. They have boycotted the trial for the past six weeks after it was disclosed that conversations between the defendants and their lawyers were bugged by authorities.

The Buback killing has given support to authorities here who had been under fire for bugging avowed anarchists. Even Schmidt, to the displeasure of left wing of his party, has recently expressed support for monitoring, under some circumstances, discussions between lawyers and accused terrorist clients.

In the Baadar-Meinhof case, the prosecutors alleged that the lawyers were helping the defendants plan crimes from their jail cells.

The Baader-Meinhof group was born amid the student unrest of the late 1960s but went underground in the early 1970s and spread urban violence, arson and the fear of anarchy through much of West Germany's orderly and discipline-concious population.

The alleged ringleaders were arrested in 1972, but were held for three years in pre-trial detention, much of it in isolation. The prosecutors claimed that they needed time to build an air-tight case.

In addition to the life sentences, the highest sentence in West Germany, the court also imposed 15-year sentences on the defendants for bomb attacks on two police stations, a publishing house and judge's car, and for attempted murder of policemen during a shoot-out when they were arrested.

The defendants are said to be in poor physical condition as a result of a hunger strike and are said to be on the brink of requiring forced feeding to keep them alive.