I heard a quote once, and sadly I can't remember where it came from (but I'd love to be able to give proper credit). It went something like this: "You know, the Bible is full of all these messed up people...except this ONE guy!"
In Genesis 15 God promises Abram that he would give the land from the brook of Egypt to the Euphrates River to his descendants, even though he was nearly 100 years old and had no children. Abram asked how that could be! Still God promised that although he was childless as of yet, he would have a child...a male heir that would come from his own body. He told him his offspring would be as numerous as the stars in the night sky.
That's a pretty big promise! God keeps his promises, big and small, no matter how bad we mess up. Abram had already messed up once, back in Genesis 12 when he was in Egypt and told Pharoh that Sarai (his wife) was his sister in order to save his own skin. After this, God struck Pharaoh and his house with plagues because he had taken Abram's wife. Once Pharaoh found this out he quickly told Abram to take her and go!
Again, in Genesis 18:10, God reaffirms his promise with Abraham telling him that “I will certainly come back to you in about a year’s time, and your wife Sarah will have a son!” This time Sarah herself had overheard and even laughed to herself about having a son after she was old and shriveled up. But again, God doesn't make promises he doesn't plan to keep.
So now we fast forward what was about 3 months, and up to Genesis 20 where we find Abraham and Sarah were travelling and they settled in a place called Gerar for a while. Once again, Abraham, foolishly fearing for his life because of his beautiful wife, he told everyone that Sara was his sister. The king of Gerar, Abimelech had Sarah brought to him. Once he did that God came to Abimelech in a dream and said to him, “You are about to die because of the woman you have taken, for she is a married woman.”
So, was God planning to strike Abimelech dead because he had taken a woman that he didn't know he was married as his own wife? Abimelech pleads with God saying Didn’t he himself say to me, ‘She is my sister’? And she herself said, ‘He is my brother.’ I did this with a clear conscience and clean hands.” God always knows what's going on, so in verses 6 and 7: Then God said to him in the dream, “Yes, I know that you did this with a clear conscience. I have also kept you from sinning against Me. Therefore I have not let you touch her. Now return the man’s wife, for he is a prophet, and he will pray for you and you will live. But if you do not return her, know that you will certainly die, you and all who are yours.”
We then find out that Sarah actually is Abraham's half-sister and to protect himself he'd told Sara that if they were ever questioned, Show your loyalty to me wherever we go and say about me: ‘He’s my brother.’”
After God spoke to Abimelech, he not only took Sarah back to Abraham but he also took 1,000 pieces of silver, sheep, cattle and slaves (both Male and Female). He even gave Abraham land -- saying “Look, my land is before you. Settle wherever you want.” Also, he tells Sara “Look, I am giving your brother 1,000 pieces of silver. It is a verification of your honor to all who are with you. You are fully vindicated.” This is his way of saying "Hey, I'm very sorry. I didn't know you were married, I never took your honor, and I haven't Fathered any children by you! Please, go in peace."
Abraham prays to God and we now find out that God had sealed the wombs of Abimelech's wife and female slaves keeping them from having children. We see now that this is what God meant back in verse 7 when he told Abimelech that he would die (or that he was a dead man) -- it wasn't so much him personally, but by sealing the womb, He had ensured that Abimelech would have no children and his nation would die!
So, why would God help Abimelech out by telling him that Sarah was actually married to Abraham? Was God really just trying to keep Abimelech from sinning against him? Actually, God was making sure that his covenant with Abraham was safe because as we see in Genesis 21, just as God had promised, Sarah is having a Son and she names him Isaac. God wanted there to be NO QUESTION in anyone's mind who Isaac's father was. So, even though Abraham was foolish for using Sarah to save himself once again, God kept his promise with him. Although Abimelech was about to commit adultery with Sarah, God stopped that -- not so much as to keep Abimelech from sinning against Him, but to ENSURE that the covenant was not broken!