Daniels, whose real name is Stephanie Clifford, sued Trump in April after he said a composite sketch of a man she said threatened her in 2011 to keep quiet about an alleged affair with the real estate mogul was a "con job."
Trump tweeted that the man was "nonexistent" and that Daniels was playing the "fake news media for fools." He retweeted a side-by-side photo comparing the sketch with a photo of Daniels' husband.
In an order handed down Monday, U.S. District Judge S. James Otero said Trump's statement was protected speech under the First Amendment.
"If this Court were to prevent Mr. Trump from engaging in this type of 'rhetorical hyperbole' against a political adversary, it would significantly hamper the office of the President," the judge wrote. "Any strongly worded response by a president to another politician or public figure could constitute an action for defamation. This would deprive this country of the 'discourse' common to the political process."
Daniels' attorney, Michael Avenatti, vowed to appeal the decision and said he was confident it would be reversed.
"There is something really rich in Trump relying on the First Amendment to justify defaming a woman," Avenatti said.
But the president's lawyer immediately hailed the ruling as a "total victory" for Trump.
"No amount of spin or commentary by Stormy Daniels or her lawyer, Mr. Avenatti, can truthfully characterize today's ruling in any way other than total victory for President Trump and total defeat for Stormy Daniels," Trump's attorney, Charles Harder, said in a statement.
The judge's ruling also entitles Trump to collect attorneys' fees from Daniels, but the amount that Daniels would need to pay will be determined later, Harder said.
The defamation claim is separate from another lawsuit that Daniels filed against Trump, which is continuing. Daniels was paid $130,000 as part of a nondisclosure agreement signed days before the 2016 election and is suing to dissolve that contract. Daniels has argued the agreement should be invalidated because Trump's then-personal lawyer and fixer, Michael Cohen, signed it, but Trump did not.
Lawyers for Trump and Cohen now say the deal that paid Daniels $130,000 to keep quiet was invalid, and they won't sue her for breaking it. Trump's attorney said the president never considered himself as a party to the agreement and doesn't dispute Daniels' assertion that the contract isn't valid.
While Trump and Cohen want the court to toss out the litigation as moot, Daniels' lawyer wants to keep the case alive, hoping to compel Trump to answer questions under oath about what he may have known about the deal.
Cohen pleaded guilty in August to campaign finance violations alleging he coordinated with Trump on a hush-money scheme to buy the silence of Daniels and a Playboy model who alleged affairs.
Associated Press writer Catherine Lucey contributed to this report.
Copyright 2019 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.