Why did Stalin sign an agreement with Fascist Germany, once a bitter enemy?
I would say to buy time.
Soviet Russia, in the person of its leader J. Stalin, needed time to modernize its army and to purge it of "subversive" elements. This required time and Stalin thought that he could gain some time signing a pact that would delay an almost certain war between the two nations.
Russia at that time was a very big country with incredibly long borders touching almost exclusively....enemies! In the West it had Germany, in the East Japan and in the South Turkey. This was very bad for the nation but also for the political structure and ideology ruling Soviet Union.
One of the basic lemmas of communism was the world proletarian struggle to impose communism and so, embedded in the ideology, there was the inevitability of an armed confrontation with the "capitalistic" powers, i.e., war.
Russia at this moment was in a state of military prostration: the Spanish and Finnish wars showed the inefficiency and lack of modern equipment of the Red Army. Not only this, the commanders were no better either! The few professional officers were trying with all means to hide themselves and their probable Czarist past (having studied and trained during the Czarist period) while the other officers were simply political protégées with little or no military training.
Also during this period Stalin needed time to purge its army of all the elements he considered "counter-revolutionary" leaving at the top only his "friends" (of dubious experience and ability).
A bonus of the pact was to eventually get back some of the Polish territories lost after WWI in case Germany invaded Poland (almost certain at that time).
[Molotov signing with Ribbentrop standing behind him (in black) beside Stalin (in white)]