You aren't alone in being puzzled about this question; theologians and Bible scholars have discussed it for centuries without coming up with a final answer. Remember: God is far greater than we are, and we'll never fully understand some things about Him until we get to heaven. The Apostle Paul said, "Now I know in part; then I shall know fully" (1 Corinthians 13:12).
The Bible tells us two important truths, however, that I urge you to keep in mind. First, it tells us that God is all-powerful. From the farthest galaxy to the smallest sub-atomic particle, God created absolutely everything in the universe -- including you and me. He also created the laws by which the universe is governed, and the Lord Jesus Christ continues to watch over it from heaven. The Bible says that "He is before all things, and in him all things hold together" (Colossians 1:17).
But the Bible also tells us another important truth: Evil is real, and this world is often attacked by spiritual forces that are not from God but from Satan. Their authority is limited and temporary, but it is nevertheless real, and we see it on every hand.
Can we completely understand these two truths? Some day we will, but not yet. In the meantime, however, we can trust God's goodness and put our lives into His hands, no matter what is happening around us. God loves us, and we know this is true because Jesus Christ died and rose again for our salvation. Is your faith and hope in Him?