The Supreme Court ruled 5 to 4 yesterday that judges can order telephone companies to help federal law enforcement officers install devices that record the numbers of all calls made from targeted phones.

Without that help, the officers have "no conceivable way" to accomplish a surveillance authorized be a judge. Justice Bryon R. White wrote in the opinion for the court.

But a dissentisng opinion by Justice John Paul Stevens charged the majority with misinterpreting a 188-year-old law to arm federal judges with "a sweeping grant of authority entirely without precedent in our nation's history."

By a wider vote, 6 to 3 the court held that judges are empowered to authorize installation of the devices, known as pen registers, which phone companies have been using for billing and other purposes for about 30 years. Similar devices called decoders are used for touch-tone phones.

The nine justices were unanimous on a third issue: the restrictions on wiretapping imposed by a 1968 law do not apply to pen registers or decoders because the devices don't intercept phone conversations.

But the New York Telephone Co., saying a pen register needs only the attachment of headphones or a tape recorder to be used for wiretapping. resisted a judge's order that it help the FBI install one of the devices with a warning of potential indiscriminate invasions of privacy.

The second U.S. circuit Court of Appeals voted 2 to 1 to overturn the order on the ground that it "could establish a most undersirable, if not dangerous and unwise precedent for the authority of federal courts to impress unwilling aid on private third parties . . . it is a law of nature that one thing leads to another. It is better not to take the first step."

By a one-vote margin, howere, the Supreme Court basically endorsed counter arguments by the Justice Department in a brief signed by Benjamin R. Civilette, chief of the Criminal Division and the incoming deputy attorney general.

The department termed the fear of abuse of pen registers for wiretapping "unfounded." Any law enforcement officer using a pen fegister for unauthorized wiretapping "would be subject to severe criminal sanctions and civil liability," the brief said. It continued:

"To speculate that despite these sanctions violations might occur, and to transform this speculation into a basis for a court's withholding authorization, is to argue against the issuance of search warrants of any sort.

"One might as well say, for example, that a court should grant a warant to search for pistol in suspected murderer's apartment because the officers might conduct a general search for other items in violation of the Fourth Amendment."

In ruling that a federal judge can order a phone company to aid the government in installing a pen register, the Supreme Court majority relied on the ALl Writs Act of 1789. It authorizes federal judges to issue all written orders "necessary or appropriate in aid of their respective jurisdications and agreeable to the usages and principles of law."

The orders were intended to enable a court to carry out its functions, not to enable a court to command a person or a corporation to aid the government in the performance of its duties, Justice Stevens wrote in his dissent. Justices William J. Brennen, Thur-good Marshall and Justice Potter Stewart joined him.

The law "does not contain, and never before has been interpreted as containing, the open-ended grant of authority to federal courts that today's decision purports to uncover." Stevens added.

For the majority, Justice White scorned the refusal of New York Telephone "to supply the meager assistance required by the FBI" to investigate an illegal gambling venture possibly making unlawful use of Bell System phones.

The AT&T subsidiary -" a highly regulated public utility with a duty to serve the public" - itself regularly employs pen registers "without court order for the purpose of checking billing operations, detecting fraud, and preventing violations of law," White pointed out.

Stevens said that "the company probably welcomes it defeat since it will make a normal profit out of compliance with orders of this kind in the future."

In New York, At&T said its refusal to aid the government in pen register cases until the court decided the issue demonstrated its concern for "protecting the privacy of our customers' communications."

In the gambling case, U.S. Judge Charles H. Tenney in March, 1976, ordered the phone company to help the FBI install a pen register on each of two of the suspects' phones. The firm offered advice but refused to lease an unused phone line in a terminal box for the phones at an inconspicuous site distant from the suspects' apartment.

FBI agents then tried for four days to find a way to string their own wires, but couldn't figure out how to do it without alerting the suspects and revealing the investigation.

The company finally asked the judge to nullify the part of his order directing it to supply facilities and technical aid. He refused.