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.