WASHINGTON — A federal judge sided with the Trump administration on Wednesday in a ruling against 18 states that sought to compel the federal government to pay subsidies to health insurance companies for the benefit of millions of low-income people.

“It appears initially that the Trump administration has the stronger legal argument,” Judge Vince Chhabria of Federal District Court in San Francisco wrote in the ruling.

He refused to issue a preliminary injunction requested by the states, leaving the dispute to be resolved in a trial in his courtroom over the next few months.

The states, led by the attorney general of California, Xavier Becerra, contend that the payments are needed to prevent chaos and confusion in insurance markets during the annual open enrollment period, which starts on Nov. 1.

But Judge Chhabria said at a hearing on Monday that California and other states had found “a very clever way” to protect their residents against immediate harm from termination of the subsidies by President Trump. As a result, he said in his Wednesday ruling, many low-income people will be “better off or unharmed.’’

The subsidies reimburse insurers for reducing deductibles, co-payments and other out-of-pocket costs for seven million low-income people who buy midlevel silver plans on the Affordable Care Act marketplace. Under the law, insurers will still have to provide the discounts, known as cost-sharing reductions, but may be unable to collect reimbursement from the government.

Mr. Trump ordered federal officials two weeks ago to stop making the monthly payments. Another federal district judge, in Washington, ruled in 2016 that the payments were unconstitutional because Congress had never appropriated money for them.

On Wednesday, Judge Chhabria concluded that “both sides have reasonable arguments’’ on the main legal question: Whether Congress appropriated money for the cost-sharing payments.

Two senior senators, Lamar Alexander of Tennessee, a Republican, and Patty Murray of Washington, a Democrat, announced last week that they had agreed on a bill to extend the cost-sharing payments through 2019 and make it easier for states to obtain waivers from requirements of the Affordable Care Act. After initially endorsing the deal, Mr. Trump backed off a day later, and the White House demanded changes as a condition of its support.

The Congressional Budget Office said Wednesday that the Alexander-Murray bill would produce a modest reduction in federal budget deficits, but would not substantially change the number of people with coverage.

The budget office estimated that the legislation would reduce deficits by a total of $3.8 billion over the next decade. With the deficit for the last fiscal year alone reaching $666 billion, that is a relatively small number, but supporters of the bill made the most of it.

“The Congressional Budget Office has found that our proposal benefits taxpayers, it benefits consumers — not insurance companies,” said Mr. Alexander, the chairman of the Senate health committee.

The proposal has broad support from Democratic senators and at least a dozen Republican senators. But conservative Republicans in the House and the Senate have denounced it as a bailout for insurers, and Mr. Trump has sent mixed signals about whether he supports it. Representative Dave Brat, Republican of Virginia, called the measure “a nonstarter.”