District Judge Colleen Kollar-Kotelly overruled a presidential memo issued by Mr Trump, which sought to reverse an Obama administration policy change.
Ms Kollar-Kotelly's injunction returns the US military to the status quo - allowing transgender personnel to serve openly and new recruits to join up.
The case was filed against in August by a group of unnamed plaintiffs.
The judge agreed with the plaintiffs' assertion that the president's directives were "not genuinely based on legitimate concerns regarding military effectiveness or budget constraints, but are instead driven by a desire to express disapproval of transgender people generally".
She went on to say that the president's reasons for seeking to ban transgender personnel in July "do not appear to be supported by any facts" and had been rejected by the military.
Judge Kollar-Kotelly ruled against a second injunction sought by the plaintiffs, over a Trump directive prohibiting the use of military resources to fund sex reassignment surgical procedures.
She said the court did not have jurisdiction to grant the injunction because no plaintiff demonstrated that they would be substantially affected by the prohibition.
A June 2016 policy change by the Obama administration allowed transgender military personnel to serve openly and set a one-year deadline for the military to begin allowing new recruits.
In July 2017 the military delayed the accession part of the policy by six months, saying it needed more time to prepare.
Photo copyright: AFP (Caption: Mr Trump has said transgender people should not serve in the military "in an capacity")