WASHINGTON — President Trump signed a long-awaited directive on Friday that precludes transgender individuals from joining the military but gives Defense Secretary Jim Mattis wide discretion in determining whether those already in the armed forces can continue to serve.

Mr. Mattis’s decisions will be based on several criteria, including military effectiveness and budgetary concerns, a senior White House official said in briefing reporters.

Left unclear was how many of the thousands of transgender service personnel estimated to be in the military might keep serving. By putting the onus on Mr. Mattis, the president appeared to open the door to allowing at least some transgender service members to remain in the military.

Dana W. White, the chief Pentagon spokeswoman, said that Mr. Mattis had received the guidance but did not indicate how he would proceed.

Mr. Trump abruptly announced the ban last month, helping to resolve a fight in Congress over whether taxpayer dollars should be used for gender transition and hormone therapy for transgender service members. Objections from conservatives had threatened a $790 billion defense and security spending package.

Mr. Mattis has six months to develop a plan to implement Mr. Trump’s directive, which also applies to the Department of Homeland Security, where the Coast Guard is housed.

The White House official, who spoke on the condition of anonymity under White House ground rules for the briefing, described the memo as a return to policies in place before the Obama administration moved last year to allow transgender people to serve openly in the military without fear of punishment.

The official also said that the military would no longer pay for sex reassignment surgeries unless withholding such funds would harm the health of someone already transitioning.

Mr. Trump’s directive precludes transgender people from joining the military unless Mr. Mattis, in consultation with the secretary of homeland security, “provides a recommendation to the contrary that I find convincing.”

The president surprised much of the Pentagon last month when he tweeted that the American military could not afford the “tremendous medical costs and disruption” of including transgender members.

Advocates for transgender service members vowed to push back, arguing that the president was disguising discrimination as concern for military readiness.

“Imagine, if you would, if the president tried to pull the same prank on Jewish soldiers or gay and lesbian soldiers or Chinese soldiers or African-American soldiers,” said Aaron Belkin, the director of the Palm Center, an organization that successfully lobbied in 2016 to lift the ban on transgender service in the military. “To pull the rug out from under a group of service members who have been defending our country is inconsistent with two centuries of American history.”

Mr. Trump won praise from social conservatives.

“President Trump is doing what he promised: putting the military’s focus where it belongs — fighting and winning wars,” Tony Perkins, a Marine veteran and president of the conservative Family Research Council, said in a statement. “Political correctness doesn’t win wars — and the president is ending policies that pretend it does.”

Capt. Jennifer Peace, 32, said Mr. Trump’s announcement of the ban last month prompted her to tell her new brigade commander that she transitioned three years ago — a fact that she said was not relevant until the policy change.

“The only thing that I’ve ever asked for is to be treated like every other soldier,” said Captain Peace, who has been deployed to both Iraq and Afghanistan.

She said that the ban has brought anxiety both to her family and to the unit of 70 people she leads at Joint Base Lewis-McChord in Washington State, and that the new directive fell short of allaying their concerns.

“The clarity isn’t there yet,” Captain Peace said of the memo. “How is my deployability any different from anyone else’s? I am as capable as anyone in my unit.”

Mr. Trump cast himself as a defender of gay and transgender rights during the 2016 presidential campaign. But more recently, he has sought to address the concerns of conservatives in Congress, who objected to paying for gender transition and hormone therapy for transgender service members.

The directive requires Mr. Mattis to submit a plan by Feb. 21 for implementing the new policy, including how to address transgender individuals already serving in the armed forces.

In deciding whether any transgender service personnel can stay in the military, the directive says, Mr. Mattis must weigh considerations of “military effectiveness,” “lethality” and “budgetary constraints.”

“Until the secretary has make that determination, no action may be taken against such individuals,” adds the directive.

Late Friday night, Senator John McCain, Republican of Arizona and chairman of the Senate Armed Services Committee, said that the Pentagon should finish reviewing the impact of transgender recruits before any policy changes were made.

“It would be a step in the wrong direction to force currently serving transgender individuals to leave the military solely on the basis of their gender identity rather than medical and readiness standards that should always be at the heart of Department of Defense personnel policy,” Mr. McCain said in a statement.

An estimated 2,000 to 11,000 active duty and reserve troops are transgender, according to a 2016 study by the RAND Corporation.

Mr. Mattis’s plan, which is to be developed in consultation with the secretary of homeland security, is to take effect by March 23.

A version of this article appears in print on , on Page A11 of the New York edition with the headline: Transgender Service Ban Gives Mattis Some Leeway. Order Reprints | Today’s Paper | Subscribe