The question is commonly asked “How can an all good and all-powerful God allow evil to exist?” but the existence of Evil and the existence of an all-powerful and all good God are not mutually exclusive. Saint Thomas Aquinas addresses this issue directly, albeit briefly, in his Summa Theologica.
As Augustine says (Enchiridion xi): “Since God is the highest good, He would not allow any evil to exist in His works, unless His omnipotence and goodness were such as to bring good even out of evil.” This is part of the infinite goodness of God, that He should allow evil to exist, and out of it produce good.St. Thomas Aquinas, Summa Theologica
Simply put. Saint Thomas is saying that God allows evil to exist to bring about greater goods. God being all knowing sees time all at once and thus can allow certain evils knowing that it will bring about a greater good.
Matt Fradd the host of the Pints with Aquinas podcast uses the example of a small child whose parents are having him vaccinated. The young child may wonder why his parents are allowing him to experience this pain. The child can’t understand the greater good, that the vaccine will protect him from some terrible disease. God, like the parents in this metaphor, allows us to experience Evil for the greater good, even though we may not be able to comprehend or understand what good could come of it.