The whole of Psalm 51 was written after Nathan had rebuked David. It must have been written shortly after, given what is stated, and David is thinking of the need for repentance, to accept God's judgment and punishment of him, and to maintain faith in God's continued goodness despite David deserving God's wrath. Indeed, when Nathan said to David, "You are the man!", David knew he had just declared that "the man" deserved death for what he'd done.
This means that despite God saying the sword would never depart from David's household, David himself would not be put to death. Instead, he would live to see the bitter fruits of his sin. Not only would some of his sons be killed later on, his own wives would be taken from him and raped. This bit of Nathan's response is significant in the way it mentions "sword" and adultery:
"Why did you despise the word of the Lord by doing what is evil in his
eyes? You struck down Uriah the Hittite with the sword and took his
wife to be your own. You killed him with the sword of the Ammonites.
Now, therefore, the sword shall never depart from your house, because
you despised me and took the wife of Uriah the Hittite to be your
own. This is what the Lord says, 'Out of your own household I am going
to bring calamity upon you. Before your very eyes I will take your
wives and give them to one who is close to you, and he will lie with
your wives in broad daylight. You did it in secret, but I will do this
thing in broad daylight before all Israel'." 2 Samuel 12:7-12 N.I.V.
1987 ed.
David would be thinking of all of that as he penned Psalm 51. That is why this Psalm is an object-lesson in God's covenant people truly repenting, in truly agreeing with God's judgment as right, in truly holding on to the fact of God's salvation despite external calamities given as chastisement.
He kept faith in the fact that God does not despise the 'sacrifice' of a broken spirit; a broken and contrite heart (verse 17). He kept faith in "the God who saves me", speaking in faith of his determination to sing in praise of God's righteousness, despite knowing the litany of punishments he was to endure (verses 14-15).
Although (as Nathan had prophesied, 2 Samuel 12:14) the baby about to be born to Bathsheeba would die, verses 24-25 show that after it had died, David comforted his wife, the result being the birth of their son Solomon. We are expressly told that the Lord loved this son, right from the start, and we learn later how God blessed Solomon when the kingship passed to him, by granting him exceptional wisdom and judgment, in order to rule Israel wisely. David died an old man, knowing that Solomon would build to the Lord a temple in Jerusalem that he had prepared for. The kingdom given to David continued, through Solomon, as God had promised. This was proof positive that David's salvation had not been taken away from him, God had not cast him away from his presence, nor had the Holy Spirit been taken from David. Therefore the joy of that salvation was, indeed, restored to him as he had pleaded for, in Psalm 51:10-12. Then, indeed, did David go on to teach sinners to turn to God, as the rest of his Psalms clearly show (verses 13-15).