Niccolo Machiavelli, one of the most important political philosophers that has ever existed, defended that in politics actions that disgust our common sense are often needed in order to defend the society created by the state, a political line popularly summarized in "the end justifies the means". This line was taken by the CPSU government during the period by some known as "Stalinism", perhaps the most controversial government in history, performing countless morally questionable acts to defend the revolution, condemnable for some, defensible for others.
One of these acts was the Ribbentrop-Molotov Non-Aggression Pact, which the USSR signed with Nazi Germany. I personally do not dare to judge whether it was justified or not, however in this article, I will try to explain the reasons that made the USSR sign it.
In 1938, Nazi Germany had recently annexed Austria and the Sudetenland, following its pan-German expansionist ideology, an expansionism that was beginning to worry a little bit the USSR, Great Britain and France. That is why the British and French governments met with the German and Italian governments in Munich on September 30, 1938, agreeing that Germany would annex Czechoslovakia, in exchange of not touching Poland, a negotiation in which the Soviet government was totally excluded. This was taken as an act of hostility, especially after Britain and France had previously rejected a proposal for an antifascist alliance presented by the USSR, assuming that this agreement was an advance of a future German-Italian-French alliance -britanism to destroy the USSR and its revolution. Personally, I believe that western democracies tried, since the beginning, leaving the Soviet Union alone, waiting that fascists and communists would eventually destroy each other, but this is already playing with speculation and pseudo-history, just my opinion.
Also, the realtions with the USSR and Japanese Empire were getting worse. Japan was in full-expansionist project, having already conquered Korea and China, and with its troops stationed on the borders of Siberia and Mongolia (USSR allies). Also, besides the constant fear of a war against Japan, the confrontations with the philo-Nazi regimes of Finland, Lithuania, Latvia and Estonia were also scaling, with the best units of the Red Army stationed on all those borders. Germany, on the other hand, needed a way of protecting his eastern flank, since they obviously didn't intend to keep the Munich Agreement and Poland would be at some point annexed, under the immobilism of Great Britain and with the western democracies idea of "keeping the peace".
Finally, on August 23, 1939, the Soviet Union and Nazi Germany signed the non-aggression pact, known as "Ribbentrop-Molotov Pact", in honor of the Foreign Ministers of both countries. Germany could annex Poland, in exchange of withdrawing its support for Japan, Finland and the Baltic States, and also in exchange that the USSR would get back his eastern part of the Polish state, historically Belarusian, Ukrainian and Lithuanian territories, caught up in the Polish-Soviet War of 1919. This pact totally surprised the British and French governments, who would later declare war on Germany after the Invasion of Poland on September 1st. The USSR, in the other had, was objectively more intelligent in that regard, waiting more than two weeks in order to recover the territories ravaged by Poland. This strategy also allowed the USSR to defeat the Japanese in Mongolia, forcing them to sign another non-aggression pact (which would be later decisive when Germany broke the Pact and Invaded the USSR). The Non-Agression pact also gave time to the USSR in order to prepare themselves for the future war and the expected Nazi invasion. Although the invasion caught them unprepared later on, they recovered and finally defeated fascism. It is said that without the time of preparation the pact gave to the USSR, the Nazis would have possibly conquered Moscow, and eventually the entire nation.
Summarizing, the Non-Aggression pact was mainly caused because the allies consistently ignoring USSR in previous pacts with Germany. And most importantly, it was a pact to win more time. The USSR was not prepared for war at that time, and even less prepared in two fronts. It also sent a message to the western democracies, who had been ignoring and leaving the USSR alone.
Sources: Vozpartisana (2016) & Russia Insider (2016)