The House Republicans’ maneuver did have a potential political upside for them, turning the tables on Democrats who will now be in the position of blocking a measure to keep the government open because of their opposition to funding the wall.

“Chuck Schumer and Senate Democrats now have to decide whether it is worth shutting down the government to keep us from securing our border,” Mr. McCarthy said.

But given Mr. Trump’s very public courting of a shutdown over the wall and unified Republican control of Congress, Democrats did not appear to be wavering in their position.

The Senate-passed measure would have extended the government funding for nine federal departments and several federal agencies past midnight Friday. Instead of confronting the president over his intransigence, Republican leaders fell in line.

“We want to keep the government open, but we also want to see an agreement that protects the border,” said Speaker Paul D. Ryan of Wisconsin. “We have very serious concerns about securing our border.”

It was an ignominious end to the tenure of Mr. Ryan, who just one day before had given his farewell speech lamenting the outrage-fueled nature of the political discourse and its role in scuttling compromise on major issues including immigration. Mr. Ryan, whose speakership was diminished by an inability to maneuver around the Freedom Caucus at critical moments, had long advocated a broad immigration compromise, but he spent his final hours in the post waging a futile and divisive fight to erect a border wall.

In an appropriate denouement to a Congress plagued by dysfunction — much of it at the hands of the president — demoralized Republicans about to lose their House majority were unsure until the last moment that they could even pass the wall funding measure, given how many of their defeated and retiring members have neglected to show up for votes in recent days. In the end, enough did, and only eight Republicans joined Democrats to oppose it.