This is a question which came up often when I was a referee instructor. The answer is that its a situation in which the exact rules take a back seat to custom and practice.
If we look strictly at the Laws of the Game (LOTG), we can see that the referee is to add time to each half to make up for stoppages. The amount of that time is left to the referee's discretion. Once stoppage time expires, however, the LOTG only allows time to be further extended if a penalty kick is awarded and stoppage time expires before the kick can be taken. In that case the half is to end once the kick is complete. There is no provision in the LOTG to allow a match to continue just to see if the attacking team will score a goal.
However, in custom and practice, if a team has a good attack going the referee is considered to be able to find a few more seconds of stoppage time. That is why you see so many close matches end when the attacking team loses possession and a defender belts the ball down the field.
I would add that personally, I feel the match should end regardless of whether a team has a chance to score. IMHO, the match is over and the winning team has earned their victory. However, I call the game the "custom and practice" way, as even the players on the winning team expect it, and it consequently helps with match control.
I must also add that the above refers to FIFA matches. Interscholastic rules in the US are different, and in high school play the game must end when the clock hits zero.