Devarim16:18

שֹׁפְטִים וְשֹׁטְרִים תִּתֶּן־לְךָ בְּכָל־שְׁעָרֶיךָ אֲשֶׁר יְהוָה אֱלֹהֶיךָ נֹתֵן לְךָ לִשְׁבָטֶיךָ וְשָׁפְטוּ אֶת־הָעָם מִשְׁפַּט־צֶדֶק׃

Judges and officers you shall appoint for yourself in all your towns that the Eternal your Almighty gives you, by your tribes, and they shall judge the people with righteous judgement.

Devarim 16:18

שֹׁפְטִים וְשֹׁטְרִים תִּתֶּן־לְךָ

Devarim / Deuteronomy 16:18 · Sod

Devarim 16:18

שֹׁפְטִים וְשֹׁטְרִים תִּתֶּן־לְךָ בְּכָל־שְׁעָרֶיךָ אֲשֶׁר יְהוָה אֱלֹהֶיךָ נֹתֵן לְךָ לִשְׁבָטֶיךָ וְשָׁפְטוּ אֶת־הָעָם מִשְׁפַּט־צֶדֶק׃

Judges and officers you shall appoint for yourself in all your towns that the Eternal your Almighty gives you, by your tribes, and they shall judge the people with righteous judgement.

Sod סוֹד

The judge within

You know what is right and still do otherwise. This verse can be read as a court that sits inside one person.

Shem MiShmuel: "appoint for yourself" is said to everyone, and the "gates" are the gates of the heart, the five senses. The judge in a person is the mind: it decides whether the path is straight. But that is not enough: an officer is needed, so that the heart bends its nature and obeys the mind.

How Shem MiShmuel explain it

Shem MiShmuel Judges and officers appoint for yourself

Our sages, of blessed memory, explained: "appoint for yourself" means each and every one. "In all your gates" (בְּכָל שְׁעָרֶיךָ) are the gates of the heart. And in the writings of Rabbi Chaim Vital it says that there are five senses: sight, hearing, smell, taste, touch. And these are the five gates of the heart, for all the senses carry their sensations to the heart. End of his words.

And it seems that in each single person too there are both things: judges and officers. The judge is the mind in a person, which judges all a person's ways: are they worthy, do they lean towards holiness, and is his path straight before the Eternal, or, Heaven forbid, the opposite. But this is still not enough. For even though the mind understands what to keep away from and what to draw near to, the heart must also bend its nature and obey the mind's instructions. This is the officers in a single person.

Takeaway

שְׁעָרֶיךָ means "your gates". The plain translation gives "your towns", while Shem MiShmuel reads the "gates" as the entrances to the heart.
The five senses as five gates of the heart come from the writings of Rabbi Chaim Vital, the student of the Ari.
The judge knows what is right, and the officer makes it happen. So in a person: understanding is not enough, the heart must obey.
Midrash Tanchuma on the Drash level tells of a judge who first corrected himself. Shem MiShmuel moves this court inside the person.