God created things which had free will. That means creatures which can go wrong or right. If a thing is free to be good it's also free to be bad. And free will is what has made evil possible. Why, then, did God give them free will? Because free will, though it makes evil possible, is also the only thing that makes possible any love or goodness or joy worth having. A world of automata -of creatures that worked like machines- would hardly be worth creating. The happiness which God designs for His for us is the happiness of being freely, voluntarily united to Him and to each other in an ecstasy of love and delight . And for that they've got to be free.
Of course God knew what would happen if they used their freedom the wrong way: apparently, He thought it worth the risk. (...) C.S. Lewis the Case for Christianity