The Supreme Court ruled yesterday that Congress may continue subsidizing the lobbying activities of tax-exempt veterans' organizations while banning lobbying by virtually all other tax-exempt groups, from the Girl Scouts to the National Audubon Society.

The unanimous decision upheld the 50-year-old law excusing the veterans' groups from the general prohibition on lobbying by tax-exempt organizations. The justices, reversing the appeals court in Washington, said the law was not an unconstitutional form of special treatment and did not violate freedom of speech.

The ruling was a major disappointment for thousands of tax-exempt charitable organizations seeking a reprieve from the lobbying restriction, or at least equal treatment in implementing it. If they lobby, they lose their tax-exempt status.

It was a big victory, however, for the American Legion, the Veterans of Foreign Wars and 20,000 other veterans' groups that constitute one of the most powerful lobbies in the United States. The lower court ruling that was struck down yesterday would have confronted them with ceasing their lobbying or sacrificing their lucrative exemptions. Tax-exempt organizations are freed from all federal tax liability. More important, contributions to tax-exempt groups are tax deductible. Justice William H. Rehnquist, writing for the court yesterday, characterized the exemptions as "cash grants" or "subsidies" that Congress is generally free to grant or withhold as it pleases.

"Our country has a longstanding policy of compensating veterans for their past contributions by providing them with numerous advantages," Rehnquist said of the favored treatment for those groups.

Such discrimination is permissible so long as important constitutional rights are not at stake, Rehnquist said. No rights were at stake in yesterday's case, he said, because the tax law does not prevent anyone from expressing his views on legislation; it merely declines to subsidize the lobbying with federal funds.

While Congress may not violate freedom of speech, he said, it is under no obligation to pay for it.

Yesterday's case was brought by Taxation With Representation of Washington, an organization concerned with federal tax policy. The U.S. Court of Appeals in Washington had rejected the group's free-speech claim, but agreed that the special treatment for veterans denied other groups equal treatment under the Fifth Amendment.

Justices Harry A. Blackmun, William J. Brennan Jr. and Thurgood Marshall agreed with yesterday's opinion in Regan vs. Taxation With Representation of Washington, but wrote separately to say they would feel different if the law were being used "to discourage the expression of particular views."

In other action yesterday, the court issued a strong warning to lower court judges not to punish misconduct by prosecutors by throwing out otherwise valid criminal convictions. The ruling also produced a sharp exchange among the justices, with Justice John Paul Stevens suggesting that Chief Justice Warren E. Burger had wasted the court's time in a "clumsy effort . . . . "

The controversy in the case, U.S. vs. Hasting, involves the "supervisory powers" of federal judges to reverse convictions in order to protect judicial integrity, even in the absence of a significant constitutional violation.

Kelvin Hasting and some friends were convicted of repeatedly raping and sodomizing three women in the St. Louis area in October, 1979, after forcing the women's car off the road and abducting them.

The defendants chose not to testify at their federal court trial. Under previous court rulings, prosecutors are not supposed to call a jury's attention to defendants' silence. Nevertheless, the prosecutor in the Hasting case noted during his closing statement to the jury that the defendants had not "challenged any of the rapes" during the trial.

The 7th U.S. Circuit Court of Appeals reversed the convictions, without specifying whether it found a significant constitutional violation or was merely disciplining the prosecutor for the comment.

Burger, supported by four other justices, said yesterday that he had combed the trial transcripts record in the case and found no serious constitutional problem that would have affected the jury's deliberation. He then took the lower court to task for trying to "deter future similar comments by the drastic step" of throwing out convictions involving only a "harmless" constitutional error.

Its action "failed to strike the balance between disciplining the prosecutor on the one hand, and the interest in the prompt administration of justice and the interests of victims on the other," Burger said.

Stevens, in a separate concurrence, strongly criticized Burger's handling of the case. "This court is far too busy to be spending countless hours reviewing trial transcripts," he said. "It is impossible for any member of this court to make the kind of conscientious and detailed examination of the record" required for the decision reached by Burger and the majority.

"I do not believe the prosecutor committed procedural error in this case," Stevens said. "If he did, however, I feel strongly that this court should not make a clumsy effort to avoid another trial by undertaking a function that can better be performed" by judges at a lower level.

Brennan and Marshall dissented from most of Burger's opinion.