Aetna and Humana shares were both down more than 2 percent after the news. Humana shares recovered losses and seesawed in intraday trading.
Judge John Bates of the U.S. District Court for the District of Columbia said the proposed deal would substantially lessen competition.
"The government identified 364 counties across 21 states where it argues that concentration in the Medicare Advantage market would rise above the presumptively unlawful level if the merger proceeds, and 17 counties across 3 states where that would be true in the public exchange markets," he said in court documents.
Aetna is considering an appeal over the judge's ruling, according to a company spokesman, saying "We're reviewing the opinion now and giving serious consideration to an appeal after putting forward a compelling case."
Antitrust regulators had been concerned about the impact that the $34 billion merger would have on the health insurance industry because they said it would reduce competition.
The U.S. Justice Department filed a lawsuit in July asking the court to stop the deal, arguing it would lead to higher prices for seniors and the disabled on Medicare as well as people who use the individual insurance program created under the Affordable Care Act.
The Obama administration had previously argued that the deal would create Medicare Advantage monopolies in 70 counties and increase market concentration in hundreds more across the country.
Trump has called Obamacare "a catastrophic event."
Last year, Aetna said in a letter to the U.S. Department of Justice that the insurer would exit much of the individual Obamacare insurance market if the agency challenged the merger.
Aetna and Human had previously said that their agreement to sell part of their combined business will help maintain competition.
Humana is the second-largest Medicare Advantage insurer, while Aetna is the fourth.
—CNBC's Bertha Coombs and Reuters contributed to this report.