With just minutes to spare, lawmakers averted the risk of financial calamity that would come with failing to raise the federal debt ceiling. But it required some very complicated legislative maneuvering and could put both sides at political risk in next year’s midterm elections.