WASHINGTON — House Republicans elected Representative Kevin McCarthy of California to be their leader on Wednesday, embracing continuity of leadership despite steep losses in last week’s midterm elections that cost the party a majority for the first time in eight years.

Mr. McCarthy, the current majority leader and No. 2 Republican in the chamber, will replace Speaker Paul D. Ryan of Wisconsin, who is retiring at the end of the year. He is tasked in the months ahead with charting the party’s path back to the majority in 2020. And more immediately, he will oversee Republicans’ efforts to protect President Trump from the expected onslaught of oversight, investigations and even a potential impeachment by newly empowered House Democrats.

In remarks after the secret-ballot vote, Mr. McCarthy, 53, sought to project Republican unity, pledged to “win back the suburban areas” that helped cost the party the majority, and said he would be willing to work with Democrats — up to a point.

“We will work with anybody across that aisle if they work to move America forward,” he said. “But let me be very clear: If their agenda is simply investigations, impeachment and not focused on the hard-working American public, we will be there to defend the American public.”