The nation of Israel was set up as a Theocracy – GOD ruled over the nation. Up through the days of the prophet Samuel, God would raise up judges to lead the people and/or deliver them out of the hand of the nations that oppressed them (in times of their disobedience).
But in 1 Samuel 8, we read that the Hebrews rejected God's rule over them and wanted a king like all the other nations. God gave them Saul, but he ended up just like God said he would. In his impatience and presumption, he offered a burnt offering in the place of Samuel, God's priest (1 Samuel 13). As a result, it was promised that his kingdom would not continue.
And so God raised up a man who was after His own heart. A shepherd boy. The sweet Psalmist of Israel. King David. By the time we get to 2 Samuel 7, the nation is enjoying peace and safety under the leadership of their new king. And it became David's desire to build a house for God – to replace the tabernacle (a tent) with a permanent structure for the worship and service of the Lord.
The prophet Nathan initially gave David his seal of approval but was sent back by God with the following message:
2 Samuel 7:8-17 Now therefore so shalt thou say unto my servant David, Thus saith the LORD of hosts, I took thee from the sheepcote, from following the sheep, to be ruler over my people, over Israel: And I was with thee whithersoever thou wentest, and have cut off all thine enemies out of thy sight, and have made thee a great name, like unto the name of the great men that are in the earth. Moreover I will appoint a place for my people Israel, and will plant them, that they may dwell in a place of their own, and move no more; neither shall the children of wickedness afflict them any more, as beforetime [a reaffirmation of the Abrahamic covenant], And as since the time that I commanded judges to be over my people Israel, and have caused thee to rest from all thine enemies. Also the LORD telleth thee that he will make thee an house. And when thy days be fulfilled, and thou shalt sleep with thy fathers, I will set up thy seed after thee, which shall proceed out of thy bowels, and I will establish his kingdom. He shall build an house for my name, and I will stablish the throne of his kingdom for ever. I will be his father, and he shall be my son. If he commit iniquity, I will chasten him with the rod of men, and with the stripes of the children of men: But my mercy shall not depart away from him, as I took it from Saul, whom I put away before thee. And thine house and thy kingdom shall be established for ever before thee: thy throne shall be established for ever. According to all these words, and according to all this vision, so did Nathan speak unto David.
The Lord said in 2 Chronicles 6:9 that David did well in that this was in his heart, and even though He tapped David's son Solomon for the job, notice how He rewards David's desire. David desired to build a house for God. God rewarded that desire by building a house for David.
God's unconditional covenant with David was that HIS THRONE WOULD BE ESTABLISHED FOREVER (2 Samuel 23:5; 2 Chronicles 13:5; 2 Chronicles 21:7; Psalm 89:1-4; Psalm 89:23-37). What a promise! There have been some enduring dynasties down throughout history, but God promised the rule of David's family over the nation of Israel is one that will never end.
It is interesting to get a glimpse of the fulfillment of this promise. The prophets foretell a future day when David will rule as prince over Israel from Jerusalem (Jeremiah 33:14-16; Ezekiel 34:23-25; Ezekiel 27:21-28) in the eternal kingdom established by David's son who is also his Lord (Psalm 132:11-12; Luke 1:26-33; Psalm 110:1; Matthew 22:41-45).