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.

NEW YORK, NY – SEPTEMBER 27: Prime Minister of Israel Benjamin Netanyahu addresses the United Nations General Assembly on September 27, 2018 in New York City. World leaders gathered for the 73rd annual meeting at the UN headquarters in Manhattan. (Photo by John Moore/Getty Images)

Mr. Netanyahu may be cynical but he doesn’t rig elections. He wins fairly, often against great odds, including, this time, the coming indictments against him and an understandable fatigue with his decade-old leadership, not to mention various other inter- and intraparty squabbles. But he seems to have succeeded again this time for the same reason he has dominated Israeli politics for most of the past 25 years: because when it comes to Israel’s national security, he is a leader with strategy and vision. And that is what many voters want.

In the mid-1990s, during his first term as prime minister, Mr. Netanyahu rejected the assumptions underlying the peace process with the Palestinians. At the time this was considered daringly right wing. Today, it is considered common sense in Israel, including by Mr. Netanyahu’s political rivals. Likewise, Mr. Netanyahu was one of the first politicians to recognize Iran as the main threat to Israel’s survival, and fought fiercely in international forums to get the world’s attention to this problem. Today, this view is also widely appreciated across the Israeli political spectrum.

The list goes on: In 2005, he warned that withdrawing Israeli troops from Gaza would end in disaster — and it did. He successfully resisted eight years of the Obama administration’s pressure to offer concessions to the Palestinians. He quickly forged an alliance with President Trump that has already proved to be of great benefit to Israel. In two years, Mr. Trump has moved the American Embassy from Tel Aviv to Jerusalem, withdrawn from the nuclear agreement with Iran, recognized Israel’s sovereignty over the Golan Heights, and on Monday, designated Iran’s Islamic Revolutionary Guards Corps as a terrorist organization. Blue and White tried to make this election a referendum on Mr. Netanyahu. Its campaign focused largely on the prime minister’s personal failings, the corruption accusations against him, and exhaustion with his leadership. But in Israel, security trumps all other issues. (A poll ahead of the election found voters rated security as their No. 1 concern.) Blue and White thought that by placing former Israel Defense Forces chiefs of staff at the top of the party list, it could counter Mr. Netanyahu’s image and experience as a defender of Israel, diplomatically and militarily. But the public still showed that it trusts the incumbent more.

Has Mr. Netanyahu ever been wrong when it comes to security? The truth is, many Israelis will find it hard to think of an example. And this goes not just for voters for the Likud party, or even the right-wing parties that are expected to join Likud in the next government, but even for Blue and White, which largely echoed Mr. Netanyahu’s positions on important foreign policy and national security questions. Those Israelis who do want Mr. Netanyahu gone — and yes, there are many — want him gone because of his personality, his coarsening of Israeli political discourse, his pettiness and, maybe, his corruption. Those Israelis who want Mr. Netanyahu to stay — and the election makes clear that there are many — want him to stay despite those same characteristics. They can forgive the prime minister for often being a small man, because they appreciate him as a great leader.

Shmuel Rosner, The New York Times