Do not leave your heads untrimmed… ( Leviticus 10:6)
A mourner does not normally take a haircut. Here, following the death of two of Aaron’s sons, Moses warns Aaron and his surviving sons (who were all in mourning) not to do so. A kohein was not allowed to let his hair grow wild. Now, one might think that this rule only applies to a mourner, that is, that a kohein may not show visible signs of mourning. We see that this is not the case from Leviticus 21:10, which re-states this mitzvah among the laws of the Kohein Gadol.
The basis of this mitzvah is the prestige of the Temple. It wouldn’t do to have shaggy, unkempt kohanim running around. Furthermore, the Temple is meant to be a place of joy, not a place of mourning. If Mordechai couldn’t wear sackcloth into the palace of Ahaseurus, how much more so signs of mourning should be prohibited in the House of God!
A regular kohein could let his hair grow, he just couldn’t enter the Temple untrimmed. The Kohein Gadol, however, was always in the Temple and was not allowed to let his hair grow long. And so, a regular kohein was obligated to take a haircut at least once every thirty days, while the Kohein Gadol got his hair cut every week before Shabbos (see Talmud Taanis 17a). The Torah says that if a kohein lets his hair grow wild, he is subject to death at the hands of Heaven. While it was forbidden for a kohein to enter with long hair, he was only liable to a Heavenly death sentence if he served with the wild hair.
This mitzvah applied to male kohanim in Temple times. It is discussed in the Talmud in the tractates of Taanis (17a-b) and Sanhedrin (22b, 83a-b). It is codified in the Mishneh Torah in the first chapter of Hilchos Biyas HaMikdash. It is #163 of the 365 negative mitzvos in the Rambam’s Sefer HaMitzvos.