It was done in stages, and to a great extent masterminded by Bismarck, who was head of government of Prussia, a major German State. First he pushed on the issue of Schleswig-Holstein, in southern Denmark where a sizable German minority lived. After fighting a war with Denmark alongside Austria in 1864 over possession of the area, Bismarck used the legal wrangling over who the area would fall to too engineer a war with Austria in 1866. This war would be a close run thing, but in the end Prussia won, and the resulting Peace of Prague did not result in any territorial loss for Austria to Prussia, but it excluded the Austrians from Germany and allowed Prussia to create the North German Confederation, a prototype of the German Empire. The southern German states were all required to ally with the North German Confederation.
Turning the North German Confederation into the German Empire, France, and to this end Bismarck began poisoning relations between his country and France. This culminated in the Ems Dispatch, which Bismarck modified to make it seem like the French diplomat to the North German Confederation was insulted by the King Wilhelm. This inflamed French national opinion, and France duly declared war on the North German Confederation to defend French prestige. The NGF's German allies joined it, and the resulting Franco-Prussian war (1870-1871) thus became a national war of defence directed by Bismarck's Prussia (which was still legally in existence within the NGF). France was comprehensively defeated by the NGF and it's allies, and in the treaties of Frankfurt and Versailles Alsace-Lorraine was annexed and the southern German states incorporated into the NGF, which was renamed the German Empire.
Basically, Bismarck started a war with Denmark to focus German nationalism, then another with Austria to mark Prussia's ascendancy among the German states, and then a war with France to establish Prussia as the guardian of Germany from external aggression. All the while he ensured that this would result in closer arrangements between the German kingdoms.