The first thing to be noticed is that "Germany" is a relatively recent invention, as the Holy Roman Empire lasted until 1806 (being disestablished by Napoleon). A German Confederation was formed in 1815 to substitute the vacuum caused by the end of the Empire, but failed because of the political dualism between Prussia and the Austrian Empire.
However, it was during this period that appeared the ancestral of current German flag, in the context of the Revolutions of 1848, also known as "Springtime of Peoples" or "Spring of Nations", when the black-red-gold flag was used by revolutionaries unsatisfied with the policies of the post-Congress of Vienna. Then the flag was adopted by Confederation between 1948 and 1950.
This flag was a mix of the flag of House of Hapsburg's black-gold Empire of Austria flag, with a red stripe remembering the flags of the Hanseatic cities (important ports of Northern Europe).
The confederation was disestablished in 1867, and substituted, among others, by the Northern German Confederation, that adopted a black-white-red flag:
The color set is a mix of Prussian black and white and the Hanseatic colors (red and white). In 1871, it became the flag of new German Empire.
From this date, the Germans alternated between the two flags. In 1918, the German Empire was substituted by the so-called "Republic of Weimar", that made the black-red-gold its flag. This period lasted until 1933, when Hitler revived the imperial colors for a brief time, when it was substituted by the well-known swastika; the imperial pattern appeared in many flags and emblems of the period, though. With the end of the Second World War, both West and East Germany revived the black-red-gold as the basic pattern to their flags, and it's been used even after the German reunification.
For the end, a curiosity: even although the flag isn't actually "gold", the term "black-red-yellow" or similar isn't used anymore since it was used in a derogatory way by the monarchists and Nazi. The Germans call it "black-red-gold" or, in German, "schwarz-rot-gold". Remember the tip!
Your comments are welcome.