A U.S. judge here yesterday upheld President Reagan's authority to impose a retroactive federal job hiring freeze, endorsing Reagan's action by asserting that the freeze is "essential for the well-being and general welfare of the American people."

Judge Charles R. Richey, explaining his decision to a packed courtroom, also made an oblique reference to the Watergate era to underscore his finding that Reagan acted in keeping with his legal obligations to see to it that the civil service is operated efficiently and economically.

"As we learned sadly," Richey said, it is "within the president's constitutional duty to take care that the laws of the United States be faithfully executed."

Richey's ruling dashed the hopes of about 20,000 workers who had been offered federal jobs between Reagan's election Nov. 4 and his inauguration Jan. 20. Reagan had ordered a hiring freeze on those jobs in his first official action Jan. 20, but another federal judge temporarily reinstated three employes in what amounted to the first round in the broader case involving all 20,000. The 20,000 claimed they had job commitment letters from the government.

Richey, faced with deciding this broader legal challenge, ruled yesterday, however, that the workers had simply received offers for jobs with the government, not official appointments, which the president was authorized to withdraw in connection with this responsibilities over the government's massive civil service system.

Robert M. Tobias, general counsel for the National Treasury Employees Union, said that all five employe groups involved in the suit will move immediately to ask the U.S. Court of Appeals here to review Richey's decision.

The judge's decision, which prompted groans in the courtroom, held that Reagan's actions limiting federal employment were "not only constitutional and legally permissible but, as asserted by the president, they are essential to the well being and general welfare of the American people at this time."

White House spokesman said after the ruling, "We are gratified the court has affirmed not only the president's action but we are gratified that they have recognized the emergency that prompted him to take that action."

Announcing his ruling from the bench yesterday after about two hours of legal argument, Richey said his decision was clearly compelled by the law and should not indicate any "lack of concern or compassion" for the 20,000 workers.

"This country is a government of laws and not of persons and if it was otherwise our republic . . . would not survive and could not survive," Richey said.

"Of course we're disappointed," said Laura Negin, a spokesman for the Treasury employes' union, one of the five groups, including the National Association of Government Employees, which had sued Reagan over the job freeze.

"Until you're actually parked at your desk at the job, they legally can withdraw it . . . you're at your own peril," another Treasury Union spokesman commented about Richey's ruling.

Reagan announced on Inauguration Day that he was putting the lid on future federal hiring, but what stunned the federal work force was an announcment from the Office of Management and Budget four days later that the freeze would also date back to Nov. 5, the day after Reagan was elected president. Only those persons with firm appointments would get their jobs, the announcement said.

The Treasury Union argued that thousands of persons had received letters from the government, between election day and the announcement of the freeze, which the union said gave them firm job commitments. The retroactive freeze order illegally deprived those people of their rights to those jobs, the union argued. In some cases, the union said, workers who thought they had job commitments sold their homes and moved to new locations only to discover they were out of work.

On Feb. 6, another U.S. judge in Washington temporarily reinstated three workers who actually had reported to work after the freeze was announced, but then were told to go home because they had no job. Judge John H. Pratt said then that the fact the workers had shown up for duty gave them a special interest in their jobs that could not be taken away by the government without at least proper notice and a chance to respond.

The order that put those three workers back on the job expired yesterday and Richey did not renew it.

Richey said yesterday that the workers who got letters of commitment had "mere offers that did not rise to the dinity" of official, irrevocable appointments. At the most, Richey said, these people only had an expectation that they had government jobs, which means the constitutional safeguards requiring notice and hearing before the jobs were rescinded did not apply.

"The hiring freeze is neither unconstitutional nor is it contrary to law," the judge said, "and it is supported by explicit . . . statutory authority," which says the president has responsibility to oversee the economy and efficiency of the massive civil service system.

When the hearing was over, Bobby Van Etten, 31, who had moved from Orlando, Fla., to Washington expecting a GS-15 job as an electrical engineer, said Richey's decision means he will have to "start all over again" looking for a job.