The Supreme Court ruled unanimously yesterday that a state can compel a public employee to pay a union for its services but not to contribute through the union to political candidates or "ideological activities unrelated to collective bargaining."

The decision involved a Michigan law requiring public employees, as a condition of employment, to pay a service charge in lieu of - and equal to - dues if they choose not to join the union that represents them.

Sixteen other states, the District of Columbia and numerous county and municipal governments permit the arrangement, called the agency shop. How many of the more than 12 million state and local public employees are covered by an agency-shop arrangement is unknown.

The decision was a sharp setback for "right to work" advocates and other union adversaries who had hoped for a decision striking down the agency shop for public employees. Such a decision would have slowed the growth of unions in the public sector and cast a shadow over the agency shop in the private sector.

In the opinion for the court, Justice Potter Stewart said the agency shop does not violate the constitutional rights of government employees who object on principle to unions in the public sector or to paying service charges for union activities such as collective bargaining, contract administration and grievance adjustment. But to compel contributions for political purposes is to infringe on constitutional rights, he wrote.

While agreeing with the judgment of the majority, Justice Lewis F. Powell Jr., joined by Chief Justice Warren E. Burger and Justice Harry A. Blackmum, protested that it had imposed "a sweeping limitation on First Amendment rights."

The ruling apparently makes a deduction or rebate possible only for nonunion employees "willing to step forward, declared their opposition to the union and initiate a proceeding to establish that some portion of their dues has been spent on 'ideological activities unrelated to collective bargaining,'" Powell said.

The majority opinion predicted "difficult problems in drawing lines" between collective-bargaining activities and ideological activities for which compulsory service charges are prohibited.

The decision nullified and sent back a Michigan Court of Appeals ruling denying nonmembers of the Detroit Federation of Teachers restitution of portions of service charges spent to further "political purposes."