The federal government may not bar foreign visitors from entering this country simply because they are affiliated with communist groups, a divided Supreme Court said yesterday.

The court, in a 3-to-3 vote, upheld a March 1986 ruling by a panel of the U.S. Court of Appeals here that the State Department, if it wishes to bar invited speakers solely on the basis of political affiliation, must certify to Congress that their visit would threaten national security.

Because it was a tied vote the court's action is binding only in the D.C. circuit and does not establish a national precedent. However, as a practical matter, most similar visa denial cases are likely to be filed here.

Yesterday's case involved four speakers who were invited to speak in this country but were denied visas in late 1983. The four are Nicaraguan Interior Minister Tomas Borge; former Italian general Nino Pasti, who belongs to a peace group allegedly affiliated with the Soviet Union; and two Cuban women allegedly tied to the communist party there.

The court of appeals panel ruled 2 to 1 that the 1977 McGovern Amendment to the Immigration and Nationality Act required the government to show more than communist affiliation in order to exclude the speakers. The panel sent their cases back to a lower court judge for further review.

"A visa denial based on a generalization" about an applicant's affiliations, the panel majority said, "surely qualifies as the brand of guilt by association Congress sought to check." The administration has "broad discretion over the admission and exclusion of aliens," the panel said, "but that discretion is not boundless."

The panel, with Judge Robert H. Bork in dissent, rejected Reagan administration agruments that it could exclude aliens on general foreign policy grounds without certifying to Congress that their presence would threaten national security.

The eight-member high court, waiting for a replacement for Lewis F. Powell Jr., who retired, did not announce the justices' votes.

Justice Harry A. Blackmun did not vote in the case, Reagan v. Abourezk, according to court press officer Toni House, because one of his former clerks was a party in the case. Justice Antonin Scalia apparently recused himself because, while on the appeals court here, he had voted on the government's request that the full appeals court hear the case.

In other action yesterday, the court:

Agreed to review the extent to which states may regulate charities' fund-raising activities. The case, Riley v. National Federation of the Blind, involves a law, struck down by a federal appeals court, that would have allowed state officials to review fund-raisers' books and license professional solicitors. The appeals court said the law violated the free-speech rights of the charities.

Agreed to decide whether physicians who work under a state contract may be sued for damages under federal civil rights laws. The case, West v. Atkins, involves an inmate in a North Carolina state prison who claimed an orthopedic surgeon refused to give him medication or perform corrective surgery on his torn Achilles' tendon.

A federal appeals court said the doctor could not be sued as a state official, and that, even if he were a full-time state employe, he could not be sued because he was exercising "professional discretion" in determining how to treat the prisoner.

In a victory for Saudi businessman Adnan Khashoggi, the court let stand a lower court ruling that Northrop Corp. must pay $31 million in fees -- plus about $10 million in interest -- to Khashoggi for arranging arms sales to Saudi Arabia in the early 1970s.

Agreed to decide whether federal bank regulators can temporarily remove executives from their jobs when they are accused of serious crimes. The case, Federal Deposit Insurance Corp. v. Mallin, involves an Iowa bank president who was suspended by the FDIC. A lower court ruled the FDIC had acted improperly.

Said it would review a lower court ruling that a Georgia death-row inmate could not, after his trial was over, challenge his conviction on the grounds that prosecutors improperly limited the number of blacks on juries. The lower court, in Amadeo v. Kemp, said the prisoner could not do so even though he did not learn of the improper actions until after the trial.