Then the word of the LORD came to Isaiah: 'Go and say to Hezekiah, Thus says the LORD, the God of David your father: I have heard your prayer; I have seen your tears. Behold, I will add fifteen years to your life. I will deliver you and this city out of the hand of the king of Assyria, and will defend this city.'"
(Isaiah 38:1-3, ESV)
In English, it sounds like God is saying, "I have definitely decided that you are going to die" and then changes His mind. This doesn't square with God's sovereignty and immutability. I think he is saying "Hezekiah, you have a terminal illness and under any normal circumstances, you will die" in order to impress upon Hezekiah the seriousness of the situation.
However, the situation is not just that Hezekiah doesn't want to die. His city also is being threatened by the Assyrians. But more seriously, Hezekiah at this point in time does not have a son. If he dies, it is the end not only of his family line but also of the line of David. God promised to raise up a king from the line of David who would reign forever. This is why God introduces himself as "the LORD, the God of David your father..." as a reminder to Hezekiah that God remembers his promises and will fulfill them. Hezekiah says "...like a weaver I have rolled up my life; he cuts me off from the loom..." referring not only to his own death but to the end of his line and David's line.
Hezekiah's Response to the LORD
"Behold, it was for my welfare that I had great bitterness;
but in love you have delivered my life from the pit of destruction,
for you have cast all my sins behind your back.
For Sheol does not thank you; death does not praise you;
those who go down to the pit do not hope
for your faithfulness.
The living, the living, he thanks you, as I do this day;
the father makes known to the children your faithfulness.
The LORD will save me, and we will play my music on stringed instruments
all the days of your lives, at the house of the LORD."
When it comes down to it, Hezekiah has to rely on God's grace: "In love you have delivered my life from the pit of destruction, for you have cast all my sins behind your back." He recognizes that he really can't do anything about the situation, and submits to God's ruling. But even more, he has the humility to admit that "...It was for my welfare that I had great bitterness..." He sees that God's ways are higher than his, and that the suffering he has gone through has actually been for his benefit.
"Then Isaiah said to Hezekiah, 'Hear the word of the LORD of hosts: Behold, the days are coming, when all that is in your house, and that which your fathers have stored up till this day, shall be carried to Babylon. Nothing shall be left, says the LORD. And some of your own sons, who will come from you, whom you will father, shall be taken away, and they shall be eunuchs in the palace of the king of Babylon.' Then said Hezekiah to Isaiah, 'The word of the LORD that you have spoken is good.' For he thought, "There will be peace and security in my days.'" (Isaiah 39:5-8, ESV)
God makes it clear to Hezekiah in this passage that Hezekiah WILL have an heir - "Some of your own sons, who will come from you, whom you will father..." (emphasis added). I think that is part of the reason why when God tells him that awful stuff will happen to his people and his descendants, he is okay with it - at least his line will not be cut off, and God's plan will continue even in the face of impossible odds.
As a tangent, I am not convinced that this passage is suggesting that God is punishing Hezekiah for showing off his wealth to the Babylonians by giving them eventually to the Babylonians. Certainly what he did was wrong (see 2 Chronicles 32:24-33). However, I don't think it fits in with God's character for God to (essentially) say "You showed off your wealth, huh? Well try this one on for size! They're going to get it all! Ha!". I think it is more likely that the emphasis of this section is to show Hezekiah a little bit more of God's plans - Firstly, that the line of David will not be cut off, and that there will be tough times ahead. God may reveal this information to Hezekiah to humble him and show him the deception of storing up wealth, but I don't think that it is because of Hezekiah's sin.
Jack Crabtree points out a similar passage in Luke. Zechariah, who has no children, is in the temple serving as priest when Gabriel appears to him to tell him he will have a son. Zechariah responds to the angel, "How shall I know this? For I am an old man, and my wife is advanced in years. " The angel's responds: "I am Gabriel. I stand in the presence of God, and I was sent to speak to you and to bring you this good news. And behold, you will be silent and unable to speak until the day that these things take place, because you did not believe my words, which will be fulfilled in their time."
It is generally assumed that Zechariah is being punished because he did not believe. However, he is actually asking a reasonable question: "How is this possible?" Gabriel's response of telling him he will be mute until the events come true is an ever present physical reminder to Zechariah that he will in fact have a son. So when Zechariah woke up the next morning wondering if it was all just a dream, there would be no option but to recognize that it would certainly come to pass. Zechariah would not have felt anything but joy when he realized he couldn't speak - even if it might have been a bit frustrating at times. He and Elizabeth would have desperately wanted a child, in the way of Jewish culture at that time and probably (very similarly to modern Indonesian culture) it was a sign of disgrace to have no children. But God, despite the seeming impossibility of the situation was gracious and fulfilled his plan by giving Zechariah and Elizabeth a son, just as in the case of Hezekiah.
When Gabriel appears to Mary to tell her she will have a son, her response is similar to Zechariah's: "How will this be, since I am a virgin?" God also gives her a sign, or reminder of the fulfillment of the promise. Her sign will not be that she is unable to speak, but even more obvious: she will be pregnant! And thus God would fulfill his promises, awaited by Mary, Zechariah, Hezekiah, David, and countless others who looked forward to the coming of the Messiah. Let us rejoice in God, who is gracious to us and fulfills his promises even when they seem impossible.