They have to write every character (besides the the players' own characters), including personality, knowledge, skills, intellect, emotional state, motivations, relationships, etc. They have to understand what a medieval blacksmith, farmer, soldier, barmaid, princess, etc. all understand. They are supposed to create whole towns, which requires some knowledge of politics, economics and a host of other issues. It's an effort worthy of a major novel or movie, and in fact it seems far more: Because it's non-linear, you never know which person your players will run into or which building they will walk into, so you would need to prepare, it seems, far more people and settings than are used.
On top of that, the dungeon master has to act all these characters, a one-person show that would overwhelm the greatest professional actors (and win every acting award if they pulled it off). Even more, because of the non-linear story and the chaos player decisions introduce, the dungeon master must improvise constantly and in a way that is consistent with the ongoing story and all the other characters they have portrayed and will portray.
Who has the time? It sounds like years of work. And even more incredibly, who has the skill (nobody who ever lived)? Yet it's done, and done well enough that millions enjoy it. How is this all done in reality?