“What the ruling signals is that both the Pentagon and the courts have recognized that Trump was stepping out of his lane when he tweeted,” said Aaron Belkin, the director of the Palm Center, which advocates on behalf of the transgender community in the military.

But, he added: “This could be a long process. We just don’t know what happens next.”

Since Mr. Trump gave the order, the Pentagon has slow-walked it, telling transgender members of the military that they could continue to serve openly while the Pentagon decided how to handle the ban. Last month, the Pentagon paid for gender-reassignment surgery for an active-duty military member.

Staff Sgt. Ashlee Bruce of the Air Force, who began her transition this year, said young transgender troops like her have been on edge in recent months, but noted that even after Mr. Trump announced all transgender troops would be discharged, her commanders and the medical team overseeing her transition continued to reassure her. They cleared the way for her to begin hormone treatments and have her name changed before she leaves for an assignment in South Korea in March.

“Everyone in the leadership kept saying it would be O.K.; they never wavered,” she said. Though she still has to wear a man’s uniform at work, she appeared in a dress for the first time at her squadron’s holiday party last week, with the support of her command.

“It was the first time I could show who I really was, and everyone was so great about it,” she said.

In announcing his ban, Mr. Trump tweeted that American forces could not afford the “tremendous medical costs and disruption” of transgender service members. But the RAND Corporation, in a 2016 study, found that allowing transgender people to serve openly in the military would “have minimal impact on readiness and health care costs” for the Pentagon.