God allowed David and Bathsheba to remain together, because Bathsheba became a widow. In ancient times, there was not much legal protection for widows. The Bible records that unscrupulous people would devour widows' houses. Widows were vulnerable in a patriarchal society.
"Woe to those who enact evil statutes
And to those who constantly record unjust decisions,
So as to deprive the needy of justice
And rob the poor of My people of their rights,
So that widows may be their spoil
And that they may plunder the orphans." (Isaiah 10:1-2)
Many women were homemakers and had no income in ancient times. When their husbands died, they lived on what the husband left behind. This became an opportunity for evil men to take advantage of them.
It was most likely better for Bathsheba to have someone to take care of her and give her legal protection.
David was king and still loved her. So he was the best person to take care of her. I think Bathsheba also loved David. David was king after all, well-respected, and man of God.
Because the child of their adultery died, God gave them another child who was Solomon.
When Solomon was born, he was born to married parents, not out of wedlock.
"Then David comforted his wife Bathsheba, and went in to her and lay with her; and she gave birth to a son, and he named him Solomon. Now the Lord loved him and sent word through Nathan the prophet, and he named him Jedidiah for the Lord’s sake." (2 Samuel 12:24-25)
Through this passage, we can see that David and Bathsheba loved each other. David had also become Bathsheba's legal husband. For the good of Solomon and Bathsheba, God did not tear them apart.
Even though God let David and Bathsheba be together, God punished David severely, because of the adultery and murder he committed.
"‘Now therefore, the sword shall never depart from your house, because you have despised Me and have taken the wife of Uriah the Hittite to be your wife.’ Thus says the Lord, ‘Behold, I will raise up evil against you from your own household; I will even take your wives before your eyes and give them to your companion, and he will lie with your wives in broad daylight. Indeed you did it secretly, but I will do this thing before all Israel, and under the sun.’" (2 Samuel 12:10-12)
The above prophecy came true with Absalom (David's son), who slept with David's concubines in broad daylight (2 Samuel 16:22). David also lost many children. David's first child by Bathsheba died (2 Samuel 12:15-18). Absalom also died in his rebellion against David (2 Samuel 18:14-15). David's son Amnon died (2 Samuel 13:28).
Ultimately, Jesus (Son of David) died, but His death redeems us of our sins.
This prophecy against David came true to many generations. But in the end, God redeemed David's sin through Jesus and also our sins through Jesus as well.