On August 23, 1939 it was announced that Nazi Germany and the Soviet Union had signed a non-aggression treaty. The pact has various names but was commonly known as the Nazi–Soviet Pact or the Molotov-Ribbentrop Pact. Both countries had suffered in World War I. Both ended with their monarchies toppled and both started as nascent democracies. Neither survived and both countries became ruled by authoritarian states. In Russia, the Communists seized power and for a while sought to bring about promised world-wide revolution that never happened thanks to British efforts to expose their purposes in neighboring countries. Germany fell into NAZI power because Hitler promised to end the chaos, bring order, and restore German power in Europe.
By 1939 though, it was evident Germany was on a collision course in Europe. The winds of war were certainly blowing with the French and British appeasing Germany. The view in Moscow was simple: they did not want war with Germany but saw Hitler as the means to weaken and divide Europe to their advantage. The view there was that an all out war in the West would so weaken them that they would be able to infiltrate and take over (either by stealth or force). Stalin too wanted territory and the Nazi-Soviet Pact gave his country much of what it needed: spheres of influence. Poland and Romania were divided between the two in secret protocols to the pact. And the countries of Lithuania, Latvia, Estonia, Finland were similarly divided as well. It gave them access to raw materials (iron, coal etc) and oil. All needed to build up the Soviet Union. For Hitler it was strategy. He wanted to invade Poland but did not want to risk war with the Soviets at that time. Although the officer corps had been severely depleted during the Great Purge of 1936-1938 (aided in part by the German Gestapo who created documents that implicated military officers or party officials of spying for Germany or others),the Soviet Union was not underestimated either.
The announcement of the pact was sensational. Many were aghast and surprised the two would sign such a pact. And when Hitler invaded Poland in September, Stalin invaded a few days later to claim the territory ceded to him in the secret parts of the pact.
Stalin unfortunately believed Hitler would hold up his end of the bargain. He thought Hitler would be more interested in subduing the West rather than heading East. And he also believed Hitler would not want a two front war. He was wrong. Despite getting warnings from the British and his own intelligence services, Hitler invaded on Sunday, June 22, 1941. Operation Barbarossa had as its goal to completely defeat the Soviet Union, kill its leadership, and reduce its population so that German settlers would occupy the land. The Germans had initial successes but after the failure of Battle of Moscow in 1942, the German army would be held back and a vicious war between the two countries along what was called the Eastern Front would emerge. It ended up demanding more resources and manpower that caused severe problems for Hitler. The Soviet Union was being supplied by the Allies (at great cost to those doing the dangerous supply missions from the North Atlantic to Murmansk). Ultimately as Germany became weaker after Allied forces invaded Europe and the Soviet Union pushed through towards Berlin, Hitler was forced to commit suicide rather than be captured by the Russians.
Stalin though came out ahead in the end. After expanding westward to push back the German army, the Soviet Union would hold on to the territory it gained. Communist parties would gain control in everyone of those countries creating, as Winston Churchill would say later, an Iron Curtain. East Germany, Poland, Czechoslovakia, Yugoslavia, Romania, and Bulgaria were all part of that curtain with the Soviet Union. Only Czechoslovakia would stay out of the Warsaw Pact but only because Tito had an independent streak but otherwise was a dedicated Communist. Only after the fall of Communism in Russia in 1992 would many of these states finally be liberated from the Soviet Union.