Donald J. Trump, twice impeached as president and now twice indicted since leaving the White House, surrendered to federal authorities in Miami on Tuesday and was arraigned on charges that he had put national security secrets at risk and obstructed investigators.

Mr. Trump was booked, fingerprinted and led to a courtroom on the 13th floor of the Federal District Court, where his lawyer entered a plea of not guilty on his behalf.

Sitting among the spectators about 20 feet away was Jack Smith, the special counsel overseeing the investigation that led to the 38-count indictment of Mr. Trump and his personal aide, Walt Nauta, who was also present for the proceedings but did not enter a plea.

Mr. Trump, who spent much of the arraignment with his arms folded and a grim expression, and Mr. Smith, a flinty former war crimes prosecutor rarely seen in public since taking charge of the case, did not talk to each other at the hearing, or even exchange glances.

The 50-minute hearing, both mundane and momentous, marked the start of what is sure to be at least a monthslong process of bringing Mr. Trump to trial against the backdrop of a presidential race in which he is the front-runner for the Republican nomination.

Mr. Trump has also been indicted in an unrelated case by the Manhattan district attorney, who has charged him in connection with hush money payments to a porn star ahead of the 2016 election. He faces a separate inquiry by a prosecutor in Fulton County, Ga., who is scrutinizing his efforts to reverse his election loss in Georgia in 2020, and Mr. Smith is pressing ahead with a federal investigation into Mr. Trump’s efforts to retain power and the ensuing Jan. 6, 2021, assault on the Capitol by a pro-Trump mob.

Outside the courthouse, amid a heavy police presence, small groups of pro-Trump demonstrators voiced their support for the former president, who has denounced the indictment as the latest installment in a long-running and politically inspired witch hunt against him.

Inside, Mr. Trump was moved briskly through the process of becoming a defendant in a federal criminal case, with the authorities seeking to minimize anything that could be interpreted as an attempt to further embarrass the former president.

He was not required to have his mug shot taken, the government did not ask for travel restrictions often imposed on those accused of serious crimes, and prosecutors seemed willing to grant him generous bond terms, without demanding cash bail.

Mr. Trump did not speak in the courtroom except for whispered chatter with his two new lawyers before the arraignment began, and asides to them once it got underway.

When asked for his plea, one of his lawyers, Todd Blanche, spoke for Mr. Trump.

“We most certainly enter a plea of not guilty,” he said.

Mr. Trump has been charged with 37 criminal counts covering seven different violations of federal law, alone or in conjunction with Mr. Nauta.

The former president was charged with 31 counts of willfully retaining national defense information under the Espionage Act and one count of making false statements stemming from his interactions with federal investigators and one of his lawyers.

Mr. Trump and Mr. Nauta were jointly charged with single counts of conspiracy to obstruct justice, withholding government documents, corruptly concealing records, concealing a document in a federal investigation and scheming to conceal their efforts. Mr. Nauta was charged with a separate count of making false statements to investigators.

Mr. Trump’s case has been assigned to Judge Aileen M. Cannon, who earlier handled a lawsuit he filed challenging the F.B.I.’s court-authorized search of his Florida estate and club, Mar-a-Lago. A ruling in Mr. Trump’s favor in that case by Judge Cannon, who was nominated by Mr. Trump, was later overturned by an appeals court that was sharply critical of her legal reasoning.

But Tuesday’s hearing was overseen by Magistrate Judge Jonathan Goodman. Magistrate judges handle many of the routine and procedural aspects of court cases.

Mr. Nauta was unable to enter a plea because he still lacked local counsel. Judge Goodman set a hearing for June 27 for Mr. Nauta to enter a plea.

Mr. Trump and Mr. Nauta were ordered by Judge Goodman not to discuss their criminal case, even though the two work closely and see each other practically every day. Judge Goodman said that any discussions related to the case must go through their lawyers.

The restrictions — which do not apply to other topics of conversation — are common for co-defendants in a criminal matter, but they could be particularly difficult to uphold given that Mr. Nauta’s job is to follow the former president through his days, attending to various needs. To underscore Mr. Nauta’s proximity to Mr. Trump, Mr. Nauta was riding with him from Mr. Trump’s club, Doral, to the courthouse for Tuesday’s hearing.

The two men talk frequently and have for most of the past two years; Mr. Nauta first served as a valet in the White House and now serves as an aide to Mr. Trump in his post-presidential life. The former president tends to treat his close personal aides as sounding boards for all manner of topics.

Mr. Trump is hardly known for his restraint under typical circumstances, but especially when told to do something by a person in a position of authority. An edict not to discuss a case that has consumed Mr. Trump’s thinking for weeks poses even more of a challenge.

The same restriction on the defendants’ communications was also applied to witnesses in the case, a list of which the government is expected to draw up. That presents a similar challenge to the situation with Mr. Nauta: A number of Mr. Trump’s advisers, current and former Mar-a-Lago staff members, and even some of his lawyers have been interviewed as part of the investigation.

The exchange also provided a glimpse into what has not yet become public about the government’s investigation — namely that a significant number of witnesses in the case, working on the president’s campaign, security detail and personal staff remain unknown to the defense.

One of the prosecutors, David Harbach, conceded that the “elephant in the room” was that the Justice Department had not yet been able to produce a comprehensive list of witnesses.

Mr. Trump’s day highlighted the challenges of being both a defendant in a criminal case that could send him to prison and a presidential candidate. And it demonstrated that Mr. Trump has no intention of muting himself as the case plays out or to abandon his instinct to fight as much in the court of public opinion as in the court of law.

Mr. Trump posted several times on his social media platform throughout the day, many of them half-sentences in which he denounced the case against him. In one post, he attacked Mr. Smith as a “thug,” while in others he kept up his long-running efforts to frame the inquiries as a partisan effort to prevent him from facing President Biden next year.

“ON MY WAY TO COURTHOUSE. WITCH HUNT!!!” he wrote at one point.

Once the court proceedings ended, Mr. Trump headed to a campaign-style stop in the Little Havana neighborhood of Miami, where his support has always been strong among Cuban Americans, and especially older Cuban exiles. “Donald Trump,” the crowd cheered. “Viva el presidente!”

He stopped into Versailles, the self-proclaimed “world’s most famous Cuban restaurant,” where he greeted a crowd of supporters, including a rabbi and a nondenominational minister who prayed on his behalf.

He then boarded his jet for a flight back to New Jersey, where he held a fund-raiser, with donors who raise at least $100,000 for his campaign invited to a “candlelight dinner,” and gave remarks at his golf club in Bedminster.

“I did everything right and they indicted me,” Mr. Trump said, subdued but barely containing his anger.

He asserted, in defiance of the clear meaning of the law, that he was entitled under the Presidential Records Act to keep the documents he took. “I had every right” to keep them, he said.

The 49-page indictment of Mr. Trump and Mr. Nauta laid out in vivid detail Mr. Trump’s casual, at times haphazard handling of highly sensitive documents from his White House. It said those documents included details of sensitive nuclear programs, intelligence on foreign adversaries, Pentagon battle plans and other documents that detailed the country’s potential vulnerabilities to military attack.

In some cases, prosecutors said, he displayed them to people without security clearances and stored them in a haphazard manner at Mar-a-Lago, even stacking a pile of boxes in a bathroom at his private club and residence in Florida.

Tuesday’s hearing also marked a milestone — it was the first time Mr. Trump and Mr. Smith, adversaries in a legal battle with enormous implications, crossed paths publicly.

After the hearing ended, Mr. Trump took a brief look over his shoulder at the reporters who crammed the courtroom, before exiting through a side door.

Mr. Smith and his prosecutors left through a door on the opposite side of the courtroom about a minute later.

Reporting was contributed by Maggie Haberman, Alan Feuer, Zach Montague, Shane Goldmacher, Nick Madigan, Nicholas Bogel-Burroughs, Frances Robles, Luke Broadwater, William K. Rashbaum, Ben Protess and Gaya Gupta.