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.