“The record reflects that he sought out the MS-13 gang and was an active, willing participant in its violent culture,” Judge Bianco wrote in the order released on Monday. More so, Mr. Portillo killed the young men, the court documents show, “with the understanding that he would be promoted within the gang.”

Mr. Portillo is the first of the accused to publicly plead guilty.

“By pleading guilty, he puts himself at the mercy of the court,” said his lawyer, Joseph W. Ryan Jr. Sentencing is scheduled for Jan. 10, 2019. “The courts recognize that sparing a trial and cost of the proceeding is an incentive to getting a more lenient sentence,” Mr. Ryan said.

The court filings added new details to the account of a gruesome night, showing that Mr. Portillo participated in planning meetings weeks before the murders when they were focusing on one victim, who in the end escaped. On April 11 Mr. Portillo set the events in motion: He texted two teenage girls, telling them where to lure the victims and relaying that information to his fellow gang members. In addition to using machetes and knives, the attackers beat their victims that night with tree limbs, the documents show.

According to court filings, the local clique of MS-13, a transnational gang formed in the 1980s in Los Angeles with refugees from El Salvador, was seeking revenge against a group of young men whom they believed to be in a rival gang. Jefferson Villalobos, a cousin of Michael Lopez Banegas, was visiting from Florida that week. The families of the slain boys have said in interviews that they were not part of any gang, and even Mr. Trump, when appearing on Long Island, recognized their parents as victims.

The quadruple homicide brought the total to 17 murders committed in Suffolk County by MS-13 from January 2016 through April 2017, and drew Mr. Trump to Long Island, twice, where he decried gang members as “animals” and urged officials to crack down on “loopholes” of the immigration system.