Dockworkers who stage "political boycotts," such as refusing to handle Soviet ship cargo to protest the invasion of Afghanistan, are acting illegally, the Supreme Court ruled yesterday.

In a unanimous decision, the court said the International Longshoremen's Association and other unions staging such illegal boycotts are subject to multimillion-dollar lawsuits by the shippers and others who lose money because of them.

"As understandable and even commendable as the ILA's ultimate objectives may be," Justice Lewis F. Powell Jr. wrote, "the certain effect of its action is to impose a heavy burden on neutral employers."

Boycotts of varying duration have become a staple of world crisis, and have been prompted in past years by repression of trade unions in Poland, terrorism in El Salvador, the death of Irish Republican Army hunger-striker Bobby Sands, and the taking of U.S. hostages in Iran.

They have irritated U.S. foreign policy officials, who believe they disrupt their own initiatives, and have occasionally been a major expense for the shipping interests caught in the boycotts.

The court's decision yesterday could put an end to them. The court said these refusals to handle cargo to and from the offending nation constitute "secondary boycotts," illegal under federal labor law with damage suits available in case of violations.

Allied International, Inc., an American importer of Russian wood products, began yesterday's case with a $15 million suit against the longshoremen's union after dockworkers refused to unload its cargo following the January, 1980, invasion of Afghanistan. Allied's case was one of several suits and National Labor Relations Board complaints stemming from that boycott.

Secondary boycotts are attempts by a union to force one company to cease doing business with another. They were made illegal under the National Labor Relations Act in order to protect neutral businesses and their employes during labor disputes.

Allied claimed that it was the neutral victim of the union's dispute with the Soviet Union. The dockworkers, supported by a U.S. District Court judge at the beginning of the case, argued in part that the 1980 protest was a political act, not a labor dispute, and that the secondary boycott prohibition was not designed for this sort of situation. The 1st U.S. Circuit Court of Appeals disagreed and its decision was appealed to the Supreme Court.

The dockworkers had "no dispute with Allied" or the shipper of the Soviet products, Powell wrote for the court. "It does not seek any labor objective from these employers. Its sole complaint is with the foreign and military policy of the Soviet Union," he said.

Powell rejected the argument that the union's free speech rights were at stake. "We have consistently rejected the claim that secondary picketing by labor unions . . . is protected activity under the First Amendment," he wrote. "It would seem even clearer that conduct designed not to communicate but to coerce merits still less consideration under the First Amendment.

"There are many ways in which a union and its individual members may express their opposition to Russian foreign policy without infringing on the rights of others."

A second case growing out of the 1980 dockworker protest awaits court action.

Yesterday's ruling in International Longshoremen's Association vs. Allied International, Inc., allows the case to proceed to trial in the lower court.

In other action yesterday:

A unanimous court declined to expand the rights of Medicare recipients to challenge rejection of medical bills they submit for services such as physical therapy, x-rays and lab tests.

The recipients had the right to a hearing before the insurance carrier administering the payment of the claims. They sought and won a U.S. District Court ruling that those hearings were inadequate because of the partiality of the carrier in such situations.

Powell, writing for the court in Schweiker vs. McClure, reversed the lower court yesterday, however, saying that the recipients had not shown that the current procedure, set up by Congress, is unfair.

In a related decision, the court unanimously ruled that businesses taking part in Medicare's voluntary supplemental insurance program cannot take their complaints about the money they receive to the federal courts.

The case, U.S. vs. Erika, Inc., stemmed from the challenge of a major supplier of kidney dialysis equipment to the reimbursement he had received for services rendered to Medicare recipients.