Mr. McCarthy clawed his way to victory by cutting a deal that won over a sizable contingent of ultraconservative lawmakers on the 12th and 13th votes earlier in the day, and then wearing down the remaining holdouts in a tense session that dragged on past midnight, ultimately winning with a bare majority, after a spectacle of arm-twisting and rancor on the House floor.