The stunning setback for Republicans underscored voter dismay over sexual misconduct claims and the limits of President Trump’s ability to sway elections.