While the king was an absolute monarch, he still had a Higher Authority he had to heed. There are quite a few mitzvos that only apply to the king. Here we are told that he could not accumulate a ridiculous quantity of horses for himself (which no doubt would have been quite the status symbol of the time). He could have what his armies required for military needs, but he was not to exceed this in order to stroke his own ego.
The reason for this mitzvah is explicit in the text: horses come from Egypt. If the king stockpiles horses, people will move to Egypt in order to engage in the horse trade. (As we will see in the very next mitzvah, we’re not allowed to live in Egypt.)
King Solomon, the smartest man who ever lived, figured that he could collect horses and still see to it that the people didn’t return to Egypt. As we see in I Kings chapter 10, people moved to Egypt to get in on the lucrative horse business. The lesson is clear: even the smartest human ever still doesn’t know better than God!
This mitzvah applies at a time when the Jews have a king. In the Talmud, it is discussed in tractate Sanhedrin on page 21b. It is codified in the Mishneh Torah in the third chapter of Hilchos Melachim. This mitzvah is #363 of the 365 negative mitzvos in the Rambam’s Sefer HaMitzvos.