“The subpoena process and investigations will be difficult,” said Marc Short, a former White House legislative director for Mr. Trump. “But there’s probably nothing that could help the president’s re-election prospects better than having Nancy Pelosi as speaker.”

[Make sense of the country’s political landscape with our newsletter.]

Former Representative Steve Israel, Democrat of New York, said the election results were a “mixed bag” for the president. “With a House Democratic majority, he has the foil he needs for his re-election campaign in 2020,” Mr. Israel said. “But no president wants the other party with subpoena power — certainly not this president.”

On the campaign trail, Mr. Trump characterized the midterm elections as a referendum on his presidency, telling supporters that they needed to vote for Republicans to continue his policies and guard him against impeachment. He made clear that he would cast a victory as a personal vindication while pre-emptively insisting that if Republicans lost, it would not be a repudiation of him.

In the days leading up to the elections, Mr. Trump emphasized that history was against him because most presidents see their party lose seats in midterm elections, in effect trying to inoculate himself against any backlash that might come with a defeat. He had a point. In the 39 midterm elections since 1862, the president’s party lost House seats 35 times and seats in the Senate 24 times.

Moreover, Mr. Trump was spared the worst outcome as Republicans held onto the Senate, ensuring his ability to continue confirming judges and other appointees. The Senate will most likely block any unfriendly legislation that House Democrats might advance, avoiding the need for the president to use his veto power.

But Republican setbacks in governor’s races in key states could complicate Mr. Trump’s re-election campaign in two years. Democrats captured the governor’s mansions in states like Kansas and Michigan that Mr. Trump won two years ago, and their control over governments in those places could make it harder for the president to repeat his feat in 2020.