The libertarian president’s ruling party staged a stunning comeback in pivotal midterm elections with unexpected support from the working class.