As with the prohibition against the king having too many horses (Mitzvah #499), we are told the reason for this mitzvah straight out: they will turn the king's heart astray. And, as with the prohibition against horses, King Solomon figured he could marry a ridiculous number of women (in order to forge treaties with other nations) but avoid the pitfall described by the Torah. As we see in I Kings chapter 11, many of these wives were insincere converts to Judaism. They erected idols in the palace, for which Solomon himself was deemed responsible. One again, even the smartest human is nowhere near as smart as God!
The maximum number of wives permitted to the king was 18. (For the record, Solomon had 700.)
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 #364 of the 365 negative mitzvos in the Rambam’s Sefer HaMitzvos.