BRENTWOOD, N.Y. — Hours after another defeat at the hands of the Republican-held Senate, President Trump launched into a speech about immigrant gang violence on Friday and wound up delivering a de facto campaign rally with a spray of Long Island police officers applauding behind him.

In his second brief appearance in his home state since taking office — this time in a small auditorium at the Suffolk County Community College in Brentwood — Mr. Trump described cities as “bloodstained killing fields” that were overrun with undocumented immigrants before his inauguration in January. He described the perpetrators of violent crimes as “animals” and said his administration seeks to “dismantle, decimate and eradicate” gangs.

He also used his roughly 30-minute speech to bolster the police, urging them to be less “nice” in arresting immigrant criminal suspects or gang members whom he described as enraptured by slow torture of their victims. The gang La Mara Salvatrucha, or MS-13, has been accused of a string of heinous gang murders on Long Island.

The Run-Up

The podcast that makes sense of the most delirious stretch of the 2016 campaign.

As expected, Mr. Trump called on Congress to fund hiring 10,000 more Immigration and Customs Enforcement agents to crack down on undocumented immigrants in the United States, including those he said pose a criminal threat, and added that the initial spending for his proposed wall on the Mexico border was just approved by the House.

Mr. Trump swung back and forth between the prepared text and a string of topics, ranging from health care, to policing in Chicago, to Mr. Trump’s campaign kickoff speech, to the need for rich people running the Treasury Department, to the desire to see law enforcement officers be a little more aggressive in arrests.

The additional immigration officers are so “we can eliminate MS-13 and root out the criminal cartels from our country,” Mr. Trump said. “Now, we’re getting them out anyway, but we’d like to get them out a lot faster. And when you see these towns, and when you see these thugs being thrown into the back of a paddy wagon, you just see them thrown in, rough — I said, ‘Please don’t be too nice.’”

He went on: “Like, when you guys put somebody in the car and you’re protecting their head, you know, the way you put your hand over” their head. “Like, don’t hit their head and they’ve just killed somebody. Don’t hit their head. I said, ‘You can take the hand away, O.K.?’”

The officers assembled behind the president applauded. But a short time later, the Suffolk County Police Department sought to distance itself from Mr. Trump’s urgings. “As a department, we do not and will not tolerate roughing up of prisoners,” it tweeted later on Friday afternoon. It said the department “has strict rules & procedures relating to the handling of prisoners. Violations of those rules are treated extremely seriously.”

At another point, Mr. Trump painted a cinematic portrait of freeing a shackled United States. “One by one, we’re liberating our American towns,” Mr. Trump said of the work to remove undocumented immigrants.

“Can you believe that I’m saying that? I’m talking about liberating our towns. Like you’d see in a movie,” Mr. Trump continued. “They’re liberating the town. Like in the old Wild West, right? We’re liberating our towns. I never thought I’d be standing up here talking about liberating towns on Long Island where I grew up. But that’s what you’re doing.”

Almost in passing, Mr. Trump criticized the Senate for refusing to approve a plan to repeal the Affordable Care Act and called for the nation’s health care law — President Barack Obama’s signature domestic achievement — to “implode.”

“They’ve been working on that one for seven years. You can believe that? The swamp. But we’ll get it done. We’re going to get it done. You know, I said from the beginning, let Obamacare implode, and then do it. I turned out to be right. Let Obamacare implode,” Mr. Trump said.

An estimated 300 protesters gathered several blocks away, outside the community college campus. Among them was Rodman Serrano, 23, a child of immigrants from El Salvador and Brentwood High School graduate, who said that Mr. Trump was spreading an exaggerated narrative that assumed all Latinos are being gang members.

Looking around the crowd of protesters, Mr. Serrano said, “This reinforces my hope that we are going to be able to fight against this president and the machine, and all the hate that’s going on.” He blamed crime on lacking school funding and after-school opportunities for youth.

Nearby stood Herby Johnson, 63, of Smithown, holding his “Bikers for Trump” helmet. He turned out to support Mr. Trump “to stop illegal immigration” and undocumented workers whom he accused of taking low-paying construction jobs and driving wages down for others in the industry.

“When they go, it will be better for everyone in construction,” Mr. Johnson said.