It was not clear what prompted Mr. Trumpâs decision. The Pentagon referred questions about the policy change to the White House, where several officials did not immediately respond to questions about the reasoning and timing behind Mr. Trumpâs decision.
But the announcement came amid a debate on Capitol Hill over the Obama-era practice of requiring the Pentagon to pay for medical treatment related to gender transition. The dispute has unfolded as Congress considers a nearly $700 billion spending bill to fund the Pentagon. Representative Vicky Hartzler, Republican of Missouri, has proposed an amendment that would bar the Pentagon from spending money on transition surgery or related hormone therapy, and other Republicans have pressed for similar provisions.
Ms. Hartzlerâs version narrowly failed this month in the House, with some Republicans joining Democrats to reject it. But some members of the conservative Freedom Caucus have indicated they would not support the military spending measure without the language banning money for gender transition.
The policy would affect only a small portion of the approximately 1.3 million active-duty members of the military. About 2,450 are transgender, according to a study last year by the RAND Corporation, though the estimated number of transgender service members has varied.
The 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. It estimated that health care costs would rise $2.4 million to $8.4 million a year, representing an infinitesimal 0.04- to 0.13 percent increase in spending for active-duty service members. Citing research into other countries that allow transgender people to serve, the study projected âlittle or no impact on unit cohesion, operational effectiveness or readinessâ in the United States.
Officials at the Pentagon were caught off guard. They had been studying, per the orders of Defense Secretary Jim Mattis, how transgender troops in the military affect other service members, but not with a view toward removing transgender people from the military, several defense officials said.
In June, the administration delayed a decision on whether to allow transgender recruits to join the military. At the time, Mr. Mattis said an extra six months would give military leaders a chance to review its potential impact. Mr. Mattisâs decision to delay accepting transgender recruits for six months had been seen as a pause to âfinesseâ the issue, one official said, not a prelude to an outright ban.
Whatâs more, Mr. Mattis loathes wading into politically divisive social policy, the official said, noting that the defense secretary, who is on vacation this week, has taken pains to steer clear of Mr. Trumpâs more partisan moves, and views the American military as a unifier of a divided country.
Gay and transgender rights groups and research organizations that have worked to craft policies around the military service of transgender individuals expressed outrage at the move.
âThe president is creating a worse version of âdonât ask, donât tell,ââ said Aaron Belkin, the director of the Palm Center, referring to the Clinton-era policy in which gay and lesbian people could not openly serve in the military.
Mr. Belkin said that âdiscreditedâ policy had harmed readiness, and Mr. Trumpâs new one would have similar effects.
âThis is a shocking and ignorant attack on our military and on transgender troops who have been serving honorably and effectively for the past year,â he added.
Joshua Block, a senior staff attorney with the American Civil Liberties Unionâs LGBT & HIV Project, called the move âan outrageous and desperate action,â and asked transgender military service members to get in touch with the organization, saying it was âexamining all our options on how to fight this.â
âThe thousands of transgender service members serving on the front lines for this country deserve better than a commander in chief who rejects their basic humanity,â Mr. Block said.
Mr. Trumpâs abrupt decision will likely end up in court; a nonprofit group that represents gay, lesbian, bisexual and transgender people in the military immediately vowed to sue.
âWe are committed to transgender service member,â the group, OutServe-SLDN, said in a statement. âWe are going to fight for them as hard as they are fighting for the country. And weâre going to start by taking the fight to Donald Trump in the federal court.â
Matthew F. Thorn, executive director of OutServe, said Mr. Trumpâs decision was a slap in the face of transgender service members.
âWe have transgender individuals who serve in elite SEAL teams, who are working in a time of war to defend our country, and now youâre going to kick them out?â Mr. Thorn said in an interview.
Mr. Carter issued a statement objecting to the decision, both for its effect on the military and on those considering joining.
âTo choose service members on other grounds than military qualifications is social policy and has no place in our military,â Mr. Carter said. âThere are already transgender individuals who are serving capably and honorably. This action would also send the wrong signal to a younger generation thinking about military service.â
And Senator John McCain, Republican of Arizona, condemned Mr. Trumpâs sudden announcement, saying it muddied policy and that anyone who is fit to serve should be allowed to do so.
âThe presidentâs tweet this morning regarding transgender Americans in the military is yet another example of why major policy announcements should not be made via Twitter,â said Mr. McCain, the chairman of the Senate Armed Services Committee.
Senator Jack Reed, Democrat of Rhode Island and the ranking member of the Armed Services Committee, noted that Mr. Trump made his decision public on the anniversary of Harry Trumanâs order desegregating the United States military. âPresident Trump is choosing to retreat in the march toward equality,â Mr. Reed said in a statement.
âThis was a divisive political move that exposes the presidentâs lack of faith in the professionalism of our armed forces,â Mr. Reed said, calling on Mr. Trump to review the facts and reverse his decision. âIn the land of the free and the home of the brave, every American who is brave enough to serve their country should be free to do so.â
An earlier version of this article misstated the president’s tweet, saying he would not “allow or accept” transgender people in the military. He tweeted he would not “accept or allow” transgender people in the military. The error was also sent in an alert.