Even though his heart was toward God, David still never understood that it was supposed to be one man for one woman. Multiple spouses is not God's ideal Way. Yet, little by little man is supposed to be moving closer to God. David had wives and was never rebuked. His son Solomon had wives and was rebuked, but primarily for having the wrong kind of wives rather than for having more than one.
Lamech is the first recorded to have taken two wives.
Jacob ends up with Leah, and then Rachel, sister wives of which was the source of a multitude of troubles.
Along comes Moses and haTorah in which it is written:
Lev 18:18 And thou shalt not take a wife to her sister, to be a rival to her
Which IMO was in part the result of Jacob and his sister wives and the troubles thereof. Of which, it is indeed possible that there were other such cases.
From the beginning, taking a near relative was not forbidden. Once the population grew enough, that was added. Just as IMO with Jacob's experience, so also the law was added not to take sisters as rival wives. Yet, there was still nothing that forbade the taking of more than one wife.
With regard to a king:
Deu 17:16a Only he shall not multiply horses to himself ... 17 Neither shall he multiply wives to himself, that his heart turn not away: neither shall he greatly multiply to himself silver and gold.
Notice in the above regarding turning his heart away as with Solomon.
It does not forbid polygamy, it only [vaguely] limits the number of wives, just as with horse and silver and gold.
Just as it was written:
Lev 18:16 Thou shalt not uncover the nakedness of thy brother's wife: it is thy brother's nakedness
Yet:
Deu 25:5 If brethren dwell together, and one of them die, and have no son, the wife of the dead shall not be married without unto a stranger: her husband's brother shall go in unto her, and take her to him to wife, and perform the duty of a husband's brother unto her.
That does not specify that he is exempted should he already have a wife.
Even before haTorah:
Gen 38:8 And Judah said unto Onan, Go in unto thy brother's wife, and perform the duty of a husband's brother unto her, and raise up seed to thy brother. 9 And Onan knew that the seed would not be his; and it came to pass, when he went in unto his brother's wife, that he spilled it on the ground, lest he should give seed to his brother. 10 And the thing which he did was evil in the sight of YHVH: and he slew him also.
When haTorah came around, the option was given to refuse:
Deu 25:7 And if the man like not to take his brother's wife, then his brother's wife shall go up to the gate unto the elders, and say, My husband's brother refuseth to raise up unto his brother a name in Israel; he will not perform the duty of a husband's brother unto me. 8 Then the elders of his city shall call him, and speak unto him: and if he stand, and say, I like not to take her; 9 then shall his brother's wife come unto him in the presence of the elders, and loose his shoe from off his foot, and spit in his face; and she shall answer and say, So shall it be done unto the man that doth not build up his brother's house. 10 And his name shall be called in Israel, The house of him that hath his shoe loosed.
Even with regard to inheritance, so also was provision made within haTorah:
Deu 21:15 If a man have two wives, the one beloved, and the other hated, and they have borne him children, both the beloved and the hated; and if the first-born son be hers that was hated;
16 then it shall be, in the day that he causeth his sons to inherit that which he hath, that he may not make the son of the beloved the first-born before the son of the hated, who is the first-born:
Edit:
Provision was made that allowed the first wife to leave if the husband did not continue to provide for her three things:
Exo 21:10 If he take him another wife; her food, her raiment, and her duty of marriage, shall he not diminish. 11 And if he do not these three things unto her, then shall she go out for nothing, without money.