Denethor went up to consult his palantír.
This is a plot twist that Tolkien spends some time setting up. Denethor implies that he knows more than anyone else, people talk about him "wrestling" in thought with Sauron, and he mysteriously vanishes to a room with a flickering light at important times:
‘Yea,’ he said; ‘for though the Stones be lost, they say, still the lords of Gondor have keener sight than lesser men, and many messages come to them. But sit now!’
The Lord of the Rings - Book V Chapter 1 - "Minas Tirith"
...the Lord Denethor is unlike other men: he sees far. Some say that as he sits alone in his high chamber in the Tower at night, and bends his thought this way and that, he can read somewhat of the future; and that he will at times search even the mind of the Enemy, wrestling with him. And so it is that he is old, worn before his time. But however that may be, my lord Faramir is abroad, beyond the River on some perilous errand, and he may have sent tidings.
The Lord of the Rings - Book V Chapter 1 - "Minas Tirith"
But he himself went up alone into the secret room under the summit of the Tower; and many who looked up thither at that time saw a pale light that gleamed and flickered from the narrow windows for a while, and then flashed and went out. And when Denethor descended again he went to Faramir and sat beside him without speaking, but the face of the Lord was grey, more deathlike than his son’s.
The Lord of the Rings - Book V Chapter 4 - "The Siege of Gondor"
Then right before Denethor's death comes the big reveal, Denethor had a palantír the whole time.
Then suddenly Denethor laughed. He stood up tall and proud again, and stepping swiftly back to the table he lifted from it the pillow on which his head had lain. Then coming to the doorway he drew aside the covering, and lo! he had between his hands a palantír. And as he held it up, it seemed to those that looked on that the globe began to glow with an inner flame, so that the lean face of the Lord was lit as with a red fire, and it seemed cut out of hard stone, sharp with black shadows, noble, proud, and terrible. His eyes glittered.
‘Pride and despair!’ he cried. ‘Didst thou think that the eyes of the White Tower were blind? Nay, I have seen more than thou knowest, Grey Fool. For thy hope is but ignorance. Go then and labour in healing! 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.’
The Lord of the Rings - Book V Chapter 7 - "The Pyre of Denethor"
After Denethor dies, Gandalf, Pippin, and Beregond explain everything to the reader.
‘Though the Stewards deemed that it was a secret kept only by themselves, long ago I guessed that here in the White Tower, one at least of the Seven Seeing Stones was preserved. In the days of his wisdom Denethor did not presume to use it, nor to challenge Sauron, knowing the limits of his own strength. 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 which 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.’
‘Now I understand what seemed so strange to me!’ said Pippin shuddering at his memories as he spoke. ‘The Lord went away from the room where Faramir lay; and it was only when he returned that I first thought he was changed, old and broken.’
‘It was in the very hour that Faramir was brought to the Tower that many of us saw a strange light in the topmost chamber,’ said Beregond. ‘But we have seen that light before, and it has long been rumoured in the City, that the Lord would at times wrestle in thought with his Enemy.’
The Lord of the Rings - Book V Chapter 7 - "The Pyre of Denethor"
In an essay on the palantíri published in Unfinished Tales, Tolkien adds further that Gandalf had suspected that Denethor had a palantír, and that this was in fact the main reason he was in such a rush to get to Minis Tirith.
But Gandalf's mind was at the same time earnestly busy with the Stones, considering the bearings of the revelation at Dol Baran upon many things that he had observed and pondered: such as the wide knowledge of events far away possessed by Denethor, and his appearance of premature old age, first observable when he was not much above sixty years old, although he belonged to a race and family that still normally had longer lives than other men. Undoubtedly Gandalf's haste to reach Minas Tirith, in addition to the urgency of the time and the imminence of war, was quickened by his sudden fear that Denethor also had made use of a palantír, the Anor-stone, and his desire to judge what effect this had had on him: whether in the crucial test of desperate war it would not prove that he (like Saruman) was no longer to be trusted and might surrender to Mordor. Gandalf's dealings with Denethor on arrival in Minas Tirith, and in the following days, and all things that they are reported to have said to one another, must be viewed in the light of this doubt in Gandalf's mind.
Unfinished Tales - "The Palantíri"