The short answer
The Bible answers this question in part, and it is honest about the part it leaves unanswered. It does not tell you why a particular sorrow came to you. It tells you who rules over that sorrow, and what he has already done about evil.
Scripture says two things together. God governs everything that happens, including the wrong that people do. And God never does evil, never tempts anyone to it, and never approves of it. The 1689 Baptist Confession keeps both in one paragraph: God’s rule reaches every sinful act, and yet he “neither is nor can be the author or approver of sin” (1689 5.4).
God is not a bystander
One common answer says God only stands back and lets evil happen. The confession refuses that answer. It says God governs evil, and “not by a bare permission” (1689 5.4). A God who only watched would set no limit on evil and give it no purpose.
Joseph is the clearest case in the Bible. His brothers sold him as a slave when he was seventeen. Years later, when he ruled Egypt and they stood in front of him afraid, he said, “As for you, you meant evil against me, but God meant it for good, to bring it about that many people should be kept alive, as they are today.” (ESV, Genesis 50:20)
Joseph called the deed evil to their faces, and the guilt of it stayed with the men who did it. In the same sentence he said God meant that same event for good.
God never does evil
A second answer says that if God governs evil, he must be behind the sin itself. Scripture refuses that as well. James writes, “God cannot be tempted with evil, and he himself tempts no one.” (ESV, James 1:13) John writes, “God is light, and in him is no darkness at all.” (ESV, 1 John 1:5)
The malice, the pride and the lie in a sinful act come from the one who commits it. If someone told you that God did the evil that was done to you, the Bible does not teach that. He is holy, and he hates it. His rule over a wrong never makes the wrong right, so you may call evil what it is, as Joseph did.
How both are true, the confession does not explain. It says both because Scripture says both.
Why did this happen to me?
Be slow with anyone who claims to know. When Job lost his children, his wealth and his health, three friends came and explained that he must have sinned. At the end of the book God says to them, “you have not spoken of me what is right” (ESV, Job 42:7).
Jesus met the same question. A tower in Siloam had fallen and killed eighteen people, and he asked the crowd, “do you think that they were worse offenders than all the others who lived in Jerusalem? No, I tell you” (ESV, Luke 13:4 to 5). He would not let anyone read the victims’ guilt off the disaster, and he gave no reason for the tower.
So this page will not tell you why your loss happened. Anyone who says he knows is guessing.
Is it wrong to ask God why?
No. The Psalms are the prayer book God gave his people, and they are full of this question. “How long, O LORD? Will you forget me forever? How long will you hide your face from me?” (ESV, Psalm 13:1)
Psalm 22 begins, “My God, my God, why have you forsaken me?” (ESV, Psalm 22:1) Jesus cried out that line from the cross. If the Son of God prayed a lament, you may pray one too. Tell God that it hurts. You do not have to finish grieving before you come to him, and you do not have to choose between your tears and your trust.
Where the answer ends: the cross
Paul takes what Joseph saw and gives it to believers as a promise: “And we know that for those who love God all things work together for good, for those who are called according to his purpose.” (ESV, Romans 8:28)
It does not say all things are good. Joseph’s pit was evil. It does not say you will see the good in this life. It is a promise to those who love God, and anyone may come to Christ and have it.
How can Paul be sure of it? Four verses later he gives his reason: “He who did not spare his own Son but gave him up for us all, how will he not also with him graciously give us all things?” (ESV, Romans 8:32)
The worst evil ever done was done to Jesus, by men who meant it for evil. God meant it for good, and by that death he saves everyone who trusts his Son. You cannot see what God means by the day you are in. Joseph could not see it from the pit. You can see the cross, and it shows you what kind of God holds that day: one who has already given what was dearest to him.
The full episode walks slowly through chapter 5 of the 1689 Confession, paragraphs 4 to 7.