The following is a paraphrase of the text of the Sefer Hachinuch, at the beginning of Mitzvah 491.
In this week’s parsha, Shoftim, we are commanded to appoint judges and law enforcers. Why? So that they will compel the people to perform the mitzvos of the Torah. What else will they do? They will force those who have diverted from the the way of truth to return to it. They will also issue edicts, clearly obligating the people to do what is required of them and ought to be done. They will, likewise, issue decrees, delineating certain practices as being unworthy of people’s occupation. And there is more. They will establish and enforce protective barriers, rules intended to safeguard people, so that they do not perform actions that are prohibited. What advantage does having these safeguards offer? It spares people the need to decide for themselves on a case by case basis. Having to exercise one’s own discretion on a continuous basis can be fraught with danger: it exposes people to the possibility of erring and succumbing to temptation. By requiring people to conform their behavior to the protective rules that serve as barriers, the judges distance the population from the pitfalls associated with coming into close contact with (spiritually) hazardous conditions. It helps reduce people’s vulnerability to falling short and transgressing.
The command to appoint judges is governed by certain specifications. Among them is a specification that mandates that judges be selected from the those in the highest tier of greatness (righteousness).