Both Hitler and Stalin when they signed the agreement knew the where going to eventually go to war. Hitler wanted to buy time so that he could focus on Britain and France before turning to Russia and Stalin knew that Russia wasn't ready for war so he wanted to buy time for Russia to mobilize and to begin supplying themselves for war and producing weapons. Even by the time war broke out and Germany invaded Russia still was under-prepared but with the help of lend-lease and their own weapons production catching up, they were able to drive back Germany.
As for the Finnish war, it would have shown how unprepared for war Russia was and would have made Hitler more confident that Germany could win.