Before we look at the two incidents where Abram said Sarai was his sister and not his wife, we need to understand that Abram lived long before God gave Moses the commandments. Abram died about 465 years before Moses was born.
Yes, God was fully aware of the pagan deities worshipped by the Egyptians and of the gross immorality of the inhabitants of Sodom and Gomorrah. But the time was not yet right for God to give His commandments to His chosen people. First, Abram and Sarai would have to produce Isaac, who then had Jacob, and from Jacob came the Hebrew people who were eventually released from Egyptian captivity. God selected Abram to be the father of His chosen people, even though Abram was far from perfect! Here is an extract from an article that explains the events in Egypt:
In Genesis 12, Abram and his wife Sarai (their names were later changed to Abraham and Sarah) travelled to Egypt due to a famine in Canaan. Abram instructed his wife to tell people in Egypt that she was his sister instead of his wife. His reason was to protect himself. Because Sarai was so beautiful, Abram feared someone would kill him and take Sarai as his wife. The plan to pass her off as his sister would ensure that Abram would be well received by those he met.
In Egypt, Sarai’s beauty attracted the attention of Pharaoh, the ruler of that country. Sarai was taken into Pharaoh’s house, and many gifts were given to Abram (Genesis 12:16). Genesis 12:17 says, “But the LORD afflicted Pharaoh and his house with great plagues because of Sarai, Abram’s wife.” This seems puzzling. After all, the king was the victim of Abram and Sarai’s deceit.
The result of this punishment reveals the reason for it. When Pharaoh realized Sarai was Abram’s wife, he summoned Abram and said, “What is this you have done to me? Why did you not tell me that she was your wife? Why did you say, ‘She is my sister,’ so that I took her for my wife? Now then, here is your wife; take her, and go” (Genesis 12:18-19). If God had not caused the plagues to come upon Pharaoh and his household, he may not have known anything was wrong. The affliction led to the discovery that Sarai was Abram’s wife. If Pharaoh had kept Sarai, Abram would not have had a son by Sarai in fulfilment of God’s promise to him (Genesis 12:2; 17:19). God graciously intervened in order to keep His covenant with Abram. https://www.gotquestions.org/Abram-Pharaoh-lie.html
Abram knew the snake-pit he was going into and to protect Sarai from a powerful, covetous ruler, and to ensure he was not murdered in order for that one to satisfy his lust with Sarai, Abram took the initiative. He made it known from the outset that she was his sister which was perfectly true, as a male and a female born to the same father were viewed as brother and sister, back then. That way, the Pharaoh would have to negotiate with Abram, and not kill him.
With regard to Abimelech, (Genesis 20:1-7) when Abraham came into a new area he occasionally feared that the local ruler would kill him and take Sarah for himself. Abraham told Abimelech king of Gerar that Sarah was his sister (she was his half-sister). Abimelech took Sarah into his harem, but God sent him a dream telling him not to touch Sarah because she was Abraham’s wife. The king returned Sarah to her husband the next morning; the dream had protected Sarah and safeguarded God’s plan for Sarah to be the mother of His chosen people.
This is what Abraham said to Abimelech in Genesis 20:11-13:
I said to myself, ‘There is surely no fear of God in this place, and they will kill me because of my wife. Besides, she really is my sister, the daughter of my father though not of my mother; and she became my wife. And when God had me wander from my father’s household, I said to her, ‘This is how you can show your love to me: Everywhere we go, say of me, “He is my brother.”
Subsequently, God healed Abimelech and his household. Abram's faith in God was such that he looked to God for divine deliverance and received it.