There are two ways to look at the question of why bad things happen to good people. First of all, technically speaking there are no good people. The Bible says in Romans 3:12, “All have turned aside. Together they have become useless. There is none who does good. There is not even one.” The reason there are none who are good is that God alone is truly good. Luke 18:19 says, “Why do you call Me good? No one is good except God alone.” God is the standard of righteousness, and all of us have fallen short of that standard (Rom. 3:23). Therefore, there really aren't any good people and bad things to happen to them.
On the other hand, from a human perspective, there are decent people who are very nice. They are honest, don't lie, don't steal, and treat people very well. So, though they aren't perfect, they are trying to do what's right. So why would God allow bad things to happen to them? The easiest answer lies in the effect of sin. Sin is in the world, and it affects everyone to different degrees. I once heard an illustration where someone said that when someone throws a grenade into a crowd, one person gets hit, but another does not. That's how sin is. It affects people differently, some a lot and some a little.
Also, God can allow bad things to happen to good people in order to teach them lessons, to discipline them, to improve their character, to encourage them to depend on him, etc. We know from the Scriptures that nothing occurs without God's permission (Ephesians 1:11). We also know that God is good, so we must conclude that he allows bad things to occur because they are according to his sovereign plan and ultimately it will work out for good - especially for those who love him (Romans 8:28).
Also, think about this. If we want God to stop bad things happening to good people, where is the line to be drawn? How good must a person be in order to be saved from bad things? Or, what level of bad do we want God to stop at? What about a bad thing that can ultimately lead to a good thing, such as the crucifixion of Jesus? Should such really bad things be stopped when they can lead to greater good? Also, what would constitute something that's bad? What is bad to one person might not be bad for another. So, ultimately, the question becomes difficult to answer the more we look at it, so the best thing to do is trust that God allows things to happen to us for a reason.