The cost of goods sold is the opening inventory plus the purchases less the closing inventory. If the closing inventory is higher, then the cost of goods sold is lower, and therefore the profit is higher.
Suppose there is no opening inventory, purchases of $1000, and closing inventory of $100. That means they only used $900 of what they bought and so the cost of goods sold is $900. Suppose instead the closing inventory was $200. That would mean they only used $800 of what they bought and so the cost of goods sold would be $800.