In a perfect world the asset would be depreciated down to exactly what we can sell it for at the end. Obviously this is impossible achieve, and if we end up selling it for less then it means we have not charged enough depreciation – the loss is effectively charging the extra depreciation and therefore reduces the profit. (And if we sell it for more, then it means we have charged too much depreciation in total, so we have a profit on sale which increases the profit).
(And of course, if we lose money for any reason then we make less profit)