At the time that the pact was signed, it was in both Germany’s and the USSR’s interest not to get involved in a war with one another. For Germany with its plans of European domination, a war on the Eastern (USSR) and Western fronts at the same time would break the effectiveness of the German Wehrmacht, whilst for the USSR war was not feasible because of the state of unpreparedness that both the Russian economy and military was in after the purges.
In Russia’s case, the pact was even more attractive seeing that it gained territorial expansion as well, because of the secret clause that was included in the pact. On top of this its economy gained by trade agreements that they entered into with Germany. The pact was not seen as a symbol of an alliance or friendship of any sorts between Germany and Russia, the two countries stood diametrically opposed to one another on some political and ideological issues, on the contrary, Hitler’s open hatred towards Russian peoples was common knowledge, and the fact that he was going to invade the USSR at some time or another was also an established fact.
The question was just when this was going to happen. In Stalin’s mind the non-aggression pact was the best way at the time to postpone the war with Germany until such a time that Russia was better equipped to handle it.
Firstly he was not ready at the time for war, and an agreement with Britain and France would mean the probable defeat of Germany at war, but at the possibility that Britain and France might leave Russia to fight Germany with little help from themselves (as their negotiations suggested), which Russia could not face in their current state of unpreparedness for war.
The fact that Stalin was reasonably assured that Hitler was going to attack the west first (Hitler was definitely not going to want to fight a war on two fronts, if he was going to attack Poland, which he was definitely going to do at the time, Britain and France would have to come to Poland’s aid (they had treaties with Poland) if Russia was not going to aid Poland. Hitler having Russia’s promise not to attack him, would therefore have his hands free to deal with the west.), combined with the fact that he also gained territory and much needed trade agreements through the pact, made the non-aggression pact much more attractive than the alliance.
He would therefore have time for as long as it takes Germany to deal with the west to prepare his own country for war. This is however where he miscalculated as Germany dealt with the west much quicker than was anticipated. .