Simply put: He was in despair because of the loss of his kingdom, the loss of his sons, and the (probable) loss of his right to rule.
- He didn't want to become Sauron's slave
- He didn't want to serve Aragorn
- He didn't want his body preserved
- He didn't have any of his sons left
- His House had effectively ended
- His Kingdom was coming to an end
Denethor knew of Gondor's hopelessness against the hordes of Mordor
Denethor had long used the Palantir of Minas Tirith. Eventually, he was ensnared by Sauron, and was slowly forced to be shown the building Might of Mordor. He finally succumbed to the fact that it was impossible to defeat Sauron.
'Go forth and fight! Vanity. For a little space you may triumph on the field, for a day. But against the Power that now arises there is no victory. To this City only the first finger of its hand has yet been stretched. All the East is moving. And even now the wind of thy hope cheats thee and wafts up Anduin a fleet with black sails. The West has failed. It is time for all to depart who would not be slaves.'
He knew he was going to lose if Sauron began open war. He knew that this was inevitable. He didn't want to become a slave of Mordor if Gondor fell.
'But his wisdom failed; and I fear that as the peril of his realm grew he looked in the Stone and was deceived: far too often, I guess, since Boromir departed. He was too great to be subdued to the will of the Dark Power, he saw nonetheless only those things which that Power permitted him to see. The knowledge that he obtained was, doubtless, often of service to him; yet the vision of the great might of Mordor that was shown to him fed the despair of his heart until it overthrew his mind.'
Denethor was in despair due to the lost of his Heirs and the impending end of his House
When we first see Denethor after Boromir's death he says:
'Dark indeed is the hour,' said the old man, 'and at such times you are wont to come, Mithrandir. But though all the signs forebode that the doom of Gondor is drawing nigh, less now to me is that darkness than my own darkness.
Secondly, when Faramir was dying:
And as he (Pippin) watched, it seemed to him that Denethor grew old before his eyes, as if something had snapped in his proud will, and his stern mind was overthrown. Grief maybe had wrought it, and remorse. He saw tears on that once tearless face, more unbearable than wrath.
Denethor started as one waking from a trance, and the flame died in his eyes, and he wept; and he said: 'Do not take my son from me! He calls for me.'
He loved his sons a lot and it pained him, an old man, to watch his sickly young son Faramir die. Not only that, the House of the Stewards was going to end at Denethor's death.
'I sent my son forth, unthanked, unblessed, out into needless peril, and here he lies with poison in his veins. Nay, nay, whatever may now betide in war, my line too is ending, even the House of the Stewards has failed.
Denethor simply didn't want to be ruled by Aragorn
Aragorn had the right to claim the Kingship of Gondor. If he had claimed it while Denethor was still alive, Denethor would have been put into the "second-highest position" in Gondor. Denethor didn't want that happening. He loved ruling Gondor by his own judgments. He loved being in power and definitely did not want to relinquish it.
'I would have things as they were in all the days of my life,' answered Denethor, 'and in the days of my longfathers before me: to be the Lord of this City in peace, and leave my chair to a son after me, who would be his own master and no wizard's pupil. But if doom denies this to me, then I will have naught: neither life diminished, nor love halved, nor honour abated.'
So when he found out that a Heir of Isildur lived, he was hella upset and disappointed, I daresay he felt more anger than despair.
'And yet in our speech together I have learned the names and purpose of all thy companions. So! With the left hand thou wouldst use me for a little while as a shield against Mordor, and with the right bring up this Ranger of the North to supplant me.
'But I say to thee, Gandalf Mithrandir, I will not be thy tool! I am Steward of the House of Anarion. I will not step down to be the dotard chamberlain of an upstart. Even were his claim proved to me, still he comes but of the line of Isildur. I will not bow to such a one, last of a ragged house long bereft of lordship and dignity.'
A bit of an overreaction to call someone an "upstart" isn't it?
As a bonus, I'd like to mention that Denethor knew who Aragorn was. Yes, the mighty Thorongil who "usurped" Ecthelion's love for Denethor in his youth was none other than Aragorn. Denethor didn't want Aragorn to rule him and so, to deny him the pleasure of "lording it over him"; he kills himself.
Denethor didn't want a "slow death"
He was proud. He felt that he was superior to other Men being the Steward of the High King. He believed that his body deserved to be burnt, not kept and preserved1.
'Why? Why do the fools fly?' said Denethor. 'Better to burn sooner than late, for burn we must. Go back to your bonfire! And I? I will go now to my pyre. To my pyre! No tomb for Denethor and Faramir. No tomb! No long slow sleep of death embalmed. We will burn like heathen kings before ever a ship sailed hither from the West. The West has failed. Go back and burn!'
Therefore, with all these factors at play, Denethor didn't feel he had a reason to live anymore.
PS: Most of the relevant quotes I've included can be readily found in the chapter: The Pyre of Denethor. The rest are scattered around in the 3rd book.
1: Kudos to @Quasi_Stomach in the comments for interpreting this better.