Stalin signed the Nazi-Soviet Pact on August 23rd, 1939, believing it would buy him time to prepare for war, even though he knew Hitler planned to eventually attack the USSR
Stalin's choices:
Alliance with Britain: would lead to war with Hitler over Poland
Alliance with Germany: would grant him half of Poland and time to prepare for the coming war with Germany
Stalin chose the latter option and signed the Pact with Hitler on August 23, 1939
The day after signing the Nazi-Soviet Pact, Stalin explained to Lavrenti Beria that he knew Hitler's intentions and believed he had tricked Hitler into thinking he was outsmarted
Consequences of the Nazi-Soviet Pact leading to the outbreak of WW2:
Freed up Hitler to invade Poland without fighting the USSR
Ended Britain's hopes of an alliance with Russia to stop Hitler, speeding up British war preparations
Gave Hitler confidence to invade Poland without fear of practical intervention from Britain and France due to their perceived weakness
Invasion of Poland timeline:
September 1st, 1939: Warsaw struck by bombing raids, German army groups invade Poland
September 2nd, 1939: Chamberlain sends ultimatum to Germany, demanding withdrawal from Poland
September 3rd, 1939: Britain declares war on Germany, followed by France, marking the beginning of WW2
September 17th, 1939: Soviet Troops invade Eastern Poland