Before and during the war Germany was officially what was known as the "German Confederation", a republic of many different independent states. The loss of the war completely destroyed the confederation.
What happened is that the army stopped fighting the Allies and attacked and invaded all the different principalities of Germany, which were all mutinying, and forced them at gunpoint to join the "Weimar Republic". The Weimar Republic thus was a civilian instrument of the armed forces of Germany, essentially a Prussian organization.
For example, in Hamburg, which before the war was a free city, one of three in Germany, starvation and unrest resulted in a Communist coup. An organization called the "Council of Workmen and Soldiers" seized power. This organization was composed of Communist agitators and deserters from the German army and navy. They established a dictatorship over Hamburg and proposed forming a new state which they called "Great Hamburg". They proposed attacking and invading nearby areas to create the so-called "Great Hamburg". One of these areas, called Cuxhaven, had been taken over in a coup by Sparticists, a form of radical, violent Communists. Hamburg absorbed them. The German army, which was Prussian-dominated, reacted to these events by invading and putting under martial law all the different states. For example, among the free cities, Bremen, which had also gone over to a Communist dictatorshop, was first to be attacked and invaded. When the "Council of Workmen and Soldiers" in Hamburg saw this at first they determined to fight and actually started trying to collect weapons as though they would have a battle against the army, but when the first troops showed up it was clear they would have no chance, so they capitulated.
In the same way all the principalities of Germany were forced into the Weimar Republic.
In most cases all the nobility had already been deposed by various radical Communist mutineers before the army even got there, but in those cases in which the local monarch still rule, the army deposed them and forced their state into the Weimar Republic. For example, in Bavaria, a Socialist political party seized power and declared the "Free State of Bavaria", which was more like a communist dictatorship than a free state. The former King of Bavaria, Ludwig III and his entire family had to flee for their lives.