"The claim that the actual world is the best of all possible worlds is the central argument in Leibniz's theodicy, or his attempt to solve the problem of evil."
It is well known that this claim can be the subject of serious criticism. One example of that criticism is the last book by the late philosopher of religion William Rowe, Can God Be Free?, of which there is a very good review by Professor Timothy O'Connor of Baylor and Indiana universities .
Quoting the possibly minimum summary of Rowe's issue from the Amazon page of the book:
This book focuses on God's freedom and praiseworthiness in relation to his perfect goodness. Given his necessary perfections, if there is a best world for God to create he would have no choice other than to create it. For, as Leibniz tells us, 'to do less good than one could is to be lacking in wisdom or in goodness'. But if God could not do otherwise than create the best world, he created the world of necessity, not freely. And, if that is so, it may be argued that we have no reason to be thankful to God for creating us, since, as parts of the best possible world, God was simply unable to do anything other than create us―-he created us of necessity, not freely. Moreover, we are confronted with the difficulty of having to believe that this world, with its Holocaust, and innumerable other evils, is the best that an infinitely powerful, infinitely good being could do in creating a world. Neither of these conclusions, taken by itself, seems at all plausible. Yet each conclusion appears to follow from the conception of God now dominant in the great religions of the West.
William Rowe presents a detailed study of this important problem, both historically in the writings of Gottfried Leibniz, Samuel Clarke, Thomas Aquinas, and Jonathan Edwards, and in the contemporary philosophical literature devoted to the issue. Rowe argues that this problem is more serious than is commonly thought and may require some significant revision in contemporary thinking about the nature of God.
I argue that the issue raised by Rowe is solved fully and simply by Christian trinitarian doctrine, according to which:
A. God the Father indeed produced the best possible world that He could: the infinitely perfect divine "world" ad-intra of the Holy Trinity, though He did it not by creation but by eternal emanation or procession, and
B. God the Father eternally generates the Son and spirates with Him the Holy Spirit by necessity of nature (*), not by a libertarian free decision.
So, God the Father indeed cannot do otherwise than produce the best world, which is the divine "world" ad-intra, and He eternally does it by necessity of nature (*).
(*) as opposed to necessity from want or necessity from external imposition.
To expand on why the doctrine of the Trinity is necessary for maximum divine goodness, I will first distinguish between the two senses in which we speak of the goodness of a person (as "good" has meaning only in the context of personal beings):
Intrinsic goodness, i.e. goodness in itself. In terms of being, to what degree of fullness it is.
Since God Is the absolute fullness of Being, He Is infinitely Good in Himself.
Moral goodness, i.e. goodness towards others. In terms of being, to what extent it causes or helps other personal beings to be in fullness. This goodness can be quantified in absolute or relative terms, the latter by comparing diffused good with intrinsic good, i.e. to what extent intrinsic good is self diffusive.
If intrinsic and moral goodness are related as per the axiom "bonum est diffusivum sui", from God's infinite intrinsic goodness we can expect Him to have (actually to Be, as per absolute divine simplicity) infinite absolute and maximal relative moral goodness.
Infinite absolute and maximal relative moral goodness implies that God eternally enunciates his perfect knowledge of Himself generating another Person Who Is the same absolute fullness of Being as He Is. It also implies that the generated Person can love the First just as the First loves Him, so that both breathe as one principle a third consubstantial Person Who Is their mutual gift of absolute Love.
As easily seen, this is just Roman Catholic trinitarian doctrine, explicitely including Filioque.
Thus, the generation of a consubstantial Son with Whom He spirates a consubstantial Holy Spirit shows that God the Father is infinitely good in absolute terms, as the emanated Persons Are the absolute fullness of Being, and maximally good in relative terms, as the emanated Persons Are all God the Father Is, except being the Father.
As the diffusion of good resulting from the generation of the Son and the spiration of the Holy Spirit is infinitely greater, both absolutely and relatively, than any diffusion of good that can result from creation, creation is not necessary and does not increase the degree of realization of divine moral goodness in the trinitarian processions, much like the addition of any finite number does not increase a transfinite number (and much less the absolute infinite, which is the mathematical analogue to God). Even the difussion of good resulting from God's elevating creatures to be "partakers of the divine nature" (2 Peter 1:4) stands to the difussion of good resulting from trinitarian processions as a transfinite number stands to the absolute infinite, i.e. the addition of the former does not increase the latter.
This demonstrates not the factuality of the Trinity, but that it is possible that God begets a consubstantial Son, and breathes with Him a consubstantial Spirit, necessarily by nature, from his being infinite Good. It is an extremely abridged exposition of St. Bonaventure's "necessary reasons" for the Trinity, based on God's nature as infinite Good and on the axiom "bonum est diffusivum sui".
 William Rowe, Can God Be Free?, Oxford University Press, 2004.