A sharply divided Supreme Court, reaffirming the First Amendment rights of public employes, ruled 5 to 4 yesterday that a county clerk in Texas could not be fired for saying that she hoped President Reagan would be assassinated.

Justice Thurgood Marshall, writing for the court, said the remark, made privately by a clerk to a fellow employe at the Harris County constable's office, was, taken in context, "political speech" and did not interfere with the functioning of the office. The clerk was fired, he said, "for exercising her constitutional right of freedom of expression."

Justice Antonin Scalia, in dissent, called the ruling irrational. "It boggles the mind," he said, "to think that she has such a right." Scalia was joined by Chief Justice William H. Rehnquist and Justices Byron R. White and Sandra Day O'Connor.

In another major ruling, the court unanimously upheld the constitutionality of a federal law allowing religious groups' nonprofit operations to hire or fire employes on the basis of their religious beliefs.

Justice Byron R. White, writing for the court in a case involving the Mormon church's firing of a janitor in a church-owned gymnasium complex in Salt Lake City, Utah, said the 1972 law's exemption of religious groups from part of the 1964 Civil Rights Act was an acceptable effort "to alleviate significant governmental interference with the ability of religious organizations to define and carry out their religious missions."

The public-employe case, Rankin v. McPherson, began on March 30, 1981, when Ardith McPherson, a keypunch operator, heard a news bulletin that President Reagan had been shot. McPherson, who is black, told her boyfriend and coworker that the would-be assassin was likely a black angered by Reagan's cuts in welfare and other social programs. "If they go for him again," she said, "I hope they get him."

A passing supervisor overheard her, and Harris County Constable Walter H. Rankin fired her, although McPherson said that the remark wasn't serious.

The case had been closely watched to see if the court would continue to restrict public employes' speech as it did in a 1983 case upholding the firing of an assistant district attorney who was organizing a protest against office policies.

American Civil Liberties Union legal director John A. Powell said yesterday's ruling gave public employes "sweeping protection" and put government employers on notice that speech "has to interfere with the workplace" before workers can be fired for it.

The court ordered McPherson's reinstatement based on her relatively low position, the fact that the remark was made in private and the fact that "the statement was made in the course of a conversation addressing the policies of the president's administration."

"There was no evidence," Marshall said, "that it interfered with the efficient functioning of the office. . . . We are not persuaded that Rankin's interest in discharging her outweighed her rights under the First Amendment."

Justice Lewis F. Powell Jr., in a brief concurring opinion, said that "on these facts, McPherson's private speech is protected by the First Amendment." Powell said that "if a statement is on a matter of public concern . . . it will be an unusual case where the employer's legitimate interests will be so great as to justify punishing an employee for this type of private speech that routinely takes place at all levels in the workplace."

Powell said it was "not easy to understand how this case assumed constitutional dimensions and reached the Supreme Court of the United States. The fact that the case is here, however," he said, showed that "courts at all levels are available and receptive to claims of injustice, large and small, by any and every citizen of this country."

Scalia said McPherson's firing was "intemperate," but said "we are not sitting as a panel to develop sound principles" to guide the state civil service. "We are asked to determine whether, given the interests of this law enforcement office, McPherson had a right to say what she did -- so that she could not only not be fired for it, but could not be formally reprimanded for it, or even prevented from repeating it endlessly into the future."

The church case involved an amendment to the 1964 Civil Rights Act that exempted religious organizations' secular activities from the law's prohibition of discrimination on the basis of religion.

In this case, Frank Mayson, the Salt Lake City janitor, was fired in 1981 after he failed to qualify for a certificate saying he was a member of the church. Such certificates are given to those who regularly attend church, tithe and abstain from coffee, tea, alcohol and tobacco.

Mayson sued, claiming that the church was violating his rights to free exercise of religion by forcing him to join or lose his job. He said the 1972 amendment allowing the church to coerce him unconstitutionally favored religion.

A federal district court judge agreed, ruling that Mayson's job had nothing to do with the religious activities of the church. The judge found the 1972 exemption impermissibly sponsored religious organizations by giving them "an exclusive authorization {to discriminate on the basis of religion} that can directly and immediately advance religious tenets and practices."

But the high court disagreed. "There is ample room" under the First Amendment, White said, citing earlier opinions, "for benevolent neutrality which will permit religious exercise to exist without sponsorship and without interference. At some point, accommodation {of religious practices} may devolve into an unlawful fostering of religion," White said, "but this is not such a case in our view."

"A law is not unconstitutional," he said, "simply because it allows churches to advance religion, which is their very purpose." For a law to be struck down, he said "it must be fair to say that the government itself has advanced religion through its own activities."

Four justices, in concurring opinions, emphasized that yesterday's ruling in Corporation of the Presiding Bishop of the Church of Jesus Christ of Latter-Day Saints v. Amos, involved an exemption only for nonprofit activities, which most often involve operations central to a church's religious mission. An exemption for those activities is more closely related to a government accommodation of religion rather than an endorsement of it, they said.