For weeks after the Senate passed a sprawling aid package for Ukraine, Israel and Taiwan, Speaker Mike Johnson agonized over whether and how the House would take up funding legislation that would almost certainly infuriate the right wing of his party and could cost him his job.

He huddled with top national security officials, including William J. Burns, the C.I.A. director, in the Oval Office to discuss classified intelligence. He met repeatedly with broad factions of Republicans in both swing and deep red districts, and considered their voters’ attitudes toward funding Ukraine. He thought about his son, who is set to attend the U.S. Naval Academy in the fall.

And finally, when his plan to work with Democrats to clear the way for aiding Ukraine met with an outpouring of venom from ultraconservatives already threatening to depose him, Mr. Johnson, an evangelical Christian, knelt and prayed for guidance.

“I want to be on the right side of history,” Representative Michael McCaul of Texas, the chairman of the Foreign Affairs Committee, recalled the speaker telling him.

Mr. Johnson’s decision to risk his speakership to push the $95 billion foreign aid bill through the House on Saturday was the culmination of a remarkable personal and political arc for the Louisiana Republican. It was also an improbable outcome for a man plucked from relative obscurity last fall by the hard right — which had just deposed a speaker they deemed a traitor to their agenda — to be the speaker of a deeply dysfunctional House.

As a rank-and-file hard-liner, Mr. Johnson had largely opposed efforts to fund Kyiv’s war effort. And early in his speakership, he declared he would never allow the matter to come to a vote until his party’s border demands were met.