The phrase comes from the early modern philosopher, G.W. Leibniz, used in his book Theodicy – though it was made famous when Voltaire later mocked it in Candide. Marc E. Bobro unpacks what it means:
In the book, Leibniz defines “world” as “the whole succession and the whole agglomeration of all existent things, lest it be said that several worlds could have existed in different times and different places. For they must needs be reckoned all together as one world or, if you will, as one Universe.” In this world, everything is dependent on something else for its existence — so that in order for the whole world to exist, a first cause must have brought it into being. But an infinite number of worlds were “equally possible,” so that in creating this world, the first cause must have been able to consider all other possible worlds. This first cause, being “infinite in all ways” — including in power, wisdom, and goodness — must have chosen the best of all possible worlds.
It is a point of interpretive controversy how close to perfection Leibniz believed the best world comes.
While most think that Leibniz considered it to be good in absolute terms, both metaphysically and morally, at least one commentator, Matthew Stewart in The Courtier and the Heretic (2006), considers Leibniz to be “in fact one of history’s great pessimists,” who recognized the vanity of striving for progress in this world that is ultimately indifferent to our desires. … But this cynical view of Leibniz’s optimism requires not only an excessively imaginative and tortuous reading of some of his most important works; it would also seem to be undermined by the dedication Leibniz brought to several other efforts, including especially his project to advance all the sciences. A proper understanding of this project reveals that Leibniz’s philosophical and theological optimism in fact shaped his vision of advancing the sciences, and that his political and ecumenical work was often aimed at furthering that end.
Leibniz made clear that he did not mean that the best world is composed only of the best parts, just as “the part of a beautiful thing is not always beautiful.” While some aspects of the world may not seem good in themselves, they are part of a whole that is better than all the alternatives. No part could in fact have been other than it is, neither better nor worse, since then the world would no longer be as it is, and this world is the best, having been chosen by an infinitely wise God.