We learn from the first chapters of the book of Genesis that God created a good world, a world where there was no evil or sin. However, it's important to realize that the potential for evil and sinful human behavior existed from the very beginning. That's because God created humans with the following characteristics:
1. First, we are personal beings, capable of living relationships with our Creator and with our fellow human beings.
2. Second, we are moral beings, with a conscience that makes us aware of the difference between right and wrong. We praise, blame, counsel, and exhort each other. Doing these things to robots is absurd. We do not hold machines morally responsible for what they do, no matter how complicated the machines are.
3. Third, we are rational beings, able to think, draw conclusions and make sensible decisions, but also with the free will to make moral choices. We are not robots, programmed to do whatever God dictates. Human beings without free will would be an animal or a machine. Instead, humans have the ability to obey God and the freedom to disobey Him.
Because God created human beings with a free will, it was up to them, not God, as to whether or not sin and moral evil would enter the world. God's plan had the potential for moral evil when He bestowed on human beings the gift of free will. Evil is inherent in God's risky gift of free will. God made moral evil possible, but humans made it actual.
The origin of moral evil came after the first humans, through an act of free will, turned away from God and towards their own selfish desires. We call the rebellion of the first man and woman (Adam and Eve) against God "The Fall." This leads to the doctrine of "original sin."
However, why did God give humans free will and then allow us to misuse it? Why should God have taken such an obvious risk in giving human beings a free will in the first place? We have free will because God wants us to choose to know, love, and serve Him. Real love - our love of God and our love of each other - must involve a free choice. Love is voluntary. It cannot be forced. True love proceeds only from a free choice. A free choice, however, leaves the possibility of a wrong choice. With God granting humans a free will came the possibility that people might instead choose to hate, not love, God and each other.
Mankind's initial and continuing rebellion against God is the source of moral evil and sin in our world. Think what a terrible thing evil and sin must be to God, since God created those people who use the gift of freedom to hurt others and even to hate God Himself.
Not even an all-powerful God could give humans a free will while guaranteeing that we would always use it wisely. That's because God could not prevent evil without removing our freedom. Not even God can bestow and withhold freedom at the same time. A person who is genuinely free and yet not free is a contradiction in terms. Our free will must include the possibility of sin and moral evil. So, God did His part perfectly; humans are the ones who messed it up.
However, this leads us to another interesting question. Since we have free will on earth, it seems likely that we will also have free will in heave. Therefore, if humans in heaven have a free will, how will there be no moral evil and sin in heaven? How can God respect our free will in heaven, but heaven still be without sin (moral evil)? In other words, how will heaven be any different from earth?!
It seems the answer is that the only humans in heaven are those who freely choose not to sin and are able to fully align their own will with God's Will. So, if we want to get into heaven, we really need to make a serious effort to deepen our union with God while here on earth, so as to allow the power and presence of the Holy Spirit to enable us to be less likely to choose sin and more likely to align our will with God's will. This helps explain why growth in holiness (sanctification) should be a primary purpose in our life.
However, if we don't completely succeed in growing in holiness while here on earth, purgatory is an additional opportunity for purification from sin and growth in holiness (sanctification) so that we may become fully ready for heaven. Even though we may be saved by Christ's atonement on the cross and will eventually go to heaven, it seems that we may not be able to actually go to heaven until we are willing and able not to sin and have learned how to fully align our will with God's Will. And, we can know God's Will by studying the Bible and seeking to learn and follow the teachings of the Church, especially as found in the Catechism of the Catholic Church.