Stalin didn't really trust Hitler. He suspected that Hitler would turn against him at some point (which he did, in 1941, when he invaded Russia with Operation Barbarossa). Signing this pact meant that he was able to buy time, which allowed Russia to build up its army ready for war