Mr. Trump traveled to Indiana three times in the two months before Election Day, helping Mike Braun oust the incumbent Democrat, Joe Donnelly. During raucous rallies, Mr. Trump falsely claimed that the Democrats wanted to “invite caravan after caravan” of asylum seekers into the United States. He added, “A blue wave will equal a crime wave, and a red wave will equal law and safety.”

It was in Indiana where the president used his finger to draw in the air the middle initial of his predecessor, Barack Hussein Obama, in an apparent nod to “birtherism,” a conspiracy theory over Mr. Obama’s citizenship that Mr. Trump has fueled.

“It’s no surprise Joe Donnelly is holding a rally this weekend with Barack H. Obama,” Mr. Trump declared with the added emphasis.

Mr. Donnelly had been seen as especially vulnerable: The president won the state of Indiana by 19 points in 2016. Mr. Donnelly tried to protect himself by veering to the right on immigration, touting his support for a border wall and for increasing cooperation between the police and immigration authorities. Toward the end of his campaign, he began to call out the “radical left,” hoping to distance himself from the most liberal members of his party.

His attempts were not enough to satisfy voters. He lost to Mr. Braun on Tuesday by a sizable margin.

But there was a clear danger on Tuesday for Republicans running in moderate battlegrounds who relied too heavily on Mr. Trump’s immigration agenda.

In Virginia, Representative Dave Brat aligned himself with Mr. Trump on the border wall and travel ban, and on Tuesday he was narrowly defeated largely because of a backlash from suburban voters. In Pennsylvania, Scott Wagner ended his Republican bid for governor with a campaign ad warning voters that “a dangerous caravan of illegals careens to the border” and that the Democratic incumbent, Tom Wolf, was doing nothing to stop it. Mr. Wagner lost by nearly 17 points.