I occasionally get the error that Word gives when trying to close a document with another dialogue box open but I've never been able to figure out why it happens.
Because most dialog boxes are considered "modal" which means that control does not pass back to the main program, or calling container, until the dialog itself is closed. This is by design and the programmer has the option to make a window modal or non-modal. Usually, a window is defined to be modal if the main program cannot or should not continue until the opened dialog is dealt-with either through selection (Ok) or aborting (Cancel).
The dialog box might be saying something like:
You have made changes to your document, do you want to save them? (Yes) (No)
There is no obvious right answer here. You may have accidentally corrupted your document (for example, the cat walked over the keyboard) in which case the answer is "No", or you might have spent hours typing in changes in which case the answer is "Yes".
The safest thing for Word to do is is refuse to close until you answer the question.
Because the program is designed this way, to avoid taking action the user might not want.
Usually, a dialog box is displayed when the program needs the user to guide some action. Closing an unsaved document is excellent example: a dialog offers to save the changes, discard the changes or abort closing and return to editing. The program intentionally refuses to close without answering this question because closing will force SOME action to be taken. The program can't decide on it's own to eg. discard recent edit, or on the contrary, overwrite correct version with cat-on-the-keyboard typing.
Even if we consider a dialog that's not related to closing, it usually means that some process is underway, it hadn't completed yet, and the user must decide which way to go. It cannot be "simply aborted", because aborting is also an action that the user might not meant.
It also simplifies design of the program, as it's creators don't have to create "a safe way out" of every function.
Today, most dialogs are not modal in the technical sense (the program remains responsible), but it's still easier to make them modal in a wider sense of logic flow of the program.
I could be mistaken, but I suspect this goes back to the old common dialog control behavior.
Several of those if killed abruptly without returning had unpleasant side effects, sometimes even outside the now-dead program and there was not a way to escape from them politely in all cases if they were doing something at a system level that required user input.
As to why it still is that way, people grew used to it, developers programmed with that assumption for decades and more importantly non-programmers used those dialogs in their office automation scripts and Microsoft is nothing if not a strict adherent to the goals backwards compatibility.
Behind the curtains, the program (in our case it's MS Word) creates a "X button click event handler" when creating the window. When there's a dialog window, Word records that. Then, in the X button handler, when the X button clicked, it checks for recorded open dialog windows. If there are some, the handler aborts the close operation. If there are not, it terminates the program and the OS cleans up the memory taken by the program. This is how it works.