Judah had three sons. His oldest son married a woman named Tamar. His son died childless, and was the custom she married his younger brother so that he could father children in the name of his older brother. The second son died before having any children. It would be the custom for her to marry the next youngest brother, but Judah was scared to allow that. He was afraid that his third son would die also. Tamar was a crafty woman. She wanted a child. She dressed herself as a prostitute and waiting along a path she knew Judah to take. She lured him in, and he had sex with her. He didn't have any way to pay her so he left his staff and seal with her. When Judah found out she was pregnant, then he had her come before him. It was then that she revealed that Judah was the baby's father.
Before the conquest of the city of Jericho, the Israelites sent spies into the city. In danger for their lives, they took refuge with a prostitute named Rahab. Rahab hid the spies and helped them to find another way out of the city. When the Israelites destroyed the city, Rahab and all of her family were spared from the destruction. Rahab was then added to the Israelites as one of her own.
After the destruction of Sodom and Gomorrah, Lot and his daughters took refuge in a cave. His daughters feared that they would never find any men for themselves. So they hatched a plan. They got their father drunk, and then had sex with him. Their descendents became the Moabites. They were a cursed people, because of the sin of Lots daughters. Later, a woman named Ruth, who is a Moabite, comes to live in the land of Israel.
King David was lounging about the castle. He happened to spy a beautiful woman taking a bath. He learned that she was the wife of one of his soldiers. He had her brought to him. The two of them had sex. In order to cover up the sin he had committed, David had Uriah, the woman's husband, sent to the front lines in battle to be killed. David was then able to marry her.
Sometimes in our failures it is hard to see how God could do anything with the mess we have made. We make bad decisions. We walk another path. It is tempting to undo a lot of the decisions we have made. I am sure we could make the case that it was a decision I made when I was far from God. It must be ok to end this relationship, or quit this particular job. I don't think we should be so hasty. The thing about all these situations that should have never happened, is that God used them to accomplish his purposes. Each of these people are included in the family tree of Jesus. Even though their descendents were brought about in ways that were anything but Godly, God still used them to accomplish his ends. It is amazing that even when we are straying God is still ready to accomplish his purposes in our lives. He is made strong in our weaknesses.