JERUSALEM — There are more than a few reasons to dislike Benjamin Netanyahu. He can be smug and vindictive. He can be ruthless when going up against political enemies. He is likely to be indicted on corruption charges related to three separate cases, which, if they are accurate, indicate that he is greedy, vain and manipulative.

And yet last night, this dislikable prime minister appears to have won his fifth — yes, fifth! — term in office. If he forms a government in the coming weeks, as he is expected to, Mr. Netanyahu will surpass Israel’s founder David Ben Gurion as the country’s longest serving prime minister. How is this possible?

To be fair, this was a close race. The main opposition party, Blue and White, is expected to get as many seats in the Knesset as Mr. Netanyahu’s Likud party. The coalition that he forms will probably have little more than the minimum 61 seats behind it.

Given Mr. Netanyahu’s unsavory qualities, many people were intent on defeating him. Just a few months ahead of the election, Blue and White, a new centrist alliance led by three decorated generals and a former security minister, came together with little purpose other than to present an alternative to Mr. Netanyahu, who has been in power since 2009. They campaigned fiercely — but civilly. At rallies, General Benny Gantz, the head of Blue and White, made it a habit to thank the prime minister for his service to the nation; this was a mirror image of Mr. Netanyahu’s and Likud’s name-calling and personal attacks. But civility and centrism weren’t enough to carry the day.