Short Answer:
Molotov–Ribbentrop Pact was always going to end badly. Staling hated Hitler and Hitler hated Stalin like no other leader in Europe. That Hitler struck first is probable not as big a mistake as it seems. Germany and the Soviet Union were always going to mix it up. Hitler's big mistake in attacking the Soviet Union was not advancing on Moscow in a timely manner and getting distracted. Most Historians split on whether the German General Staff would have been successful in Russia without Hitler's meddling. Hitler's delays in attacking Moscow meant the poorly outfitted German army had to fight through one of the coldest Russian Winters in History and the German army never recovered.
Longer Answer:
Hitler was capable of great innovative ideas, but he was also capable of huge mistakes. Most of Hitlers generals believed invading Poland was a mistake because at the time Germany didn't have much left guarding the boarder with France. Then most of his generals thought invading France was a mistake, given WWI's experience and conventional wisdom being that the French Army was the strongest on the continent, not to mention Great Britains forces. In each of these instances Hitler had proven correct despite his generals objections. One of the great debates in WWII is whether left to their own devices, without hitlers meddling could the German General staff had been successful in Russia. It's unclear, but what is clear is without Hitler they likely wouldn't have attacked the Soviet Union in June of 1941, and with Hitler they couldn't be successful.
Hitler had gained popularity in the 1920's upon four foundational points.
(1) attacking the Treaty of Versailles
(2) promoting Pan-Germanism,
(3) anti-semitism
(4) anti-communism
Hitler who was actually incredible poorly informed on many subjects which he liked to talk like an expert on, like internal American politics.. and like how influential Jewish citizens were in every country he wanted to invade. Hitler often equated anti-communism and anti-semitism as he believed communism was a jewish plot to control the world.(**) That Hitler hated communists more than the British came to news to nobody. Hitler and the Nazi's had fought street fights against communists in Germany before coming to power. He hated communism with a passion and stated as much in 1000 speeches and prominently in his own biography Mein Kamf ( my struggle). Hitler dreamed of purging Russia from Jews and Communists and capturing all that land for German use, probable in that order too.
So why did Stalin align himself with Germany. why did the Soviet Union agree to the Molotov–Ribbentrop Pact in August 1939. Because the Soviet Union was totally unprepared for war and was buying time. Stalin was trying to prepare as quickly as he could, as all of Europe was preparing. The Molotov-Ribbentrop Pact bought Russia two years, which was probable worth it for Stalin. Hitler betrayed Stalin attacking in June of 1941, because he saw an opportunity which probable didn't exist to take the Soviet Union out quickly before they could fully prepare. Which brings us back to colossal mistakes which Hitler was capable of. Yes it was a mistake to invade the Soviet Union because it caused Germany to be again be trapped in a two front war. The real fatal miscalculation was when Hitler paused and diverted forces from Army Group Centre to the south towards Kiev Ukraine. The delay stalled the attack on Moscow and forced ill-equipped(winter clothing) German troops to fight in one of the worst Russian winters ever recorded. Germany never recovered. The Wehrmacht was not able to sustain its pressure on Moscow, instead fighting, and eventually retreating, across all Russian teritory.
(*)Why was Moscow so important? I saw a previous comment about Napoleon took Moscow and it didn't help him avert a catastrophe in Russia's winter. The answer is Trains. Moscow was the central train hub for all of the Soviet Union. If Hitler had controled Moscow he would have controled all the train systems which Russia used to transport not only troops but all their military and civilian logistics. If Hitler had taken Moscow it would have been game over.
(**) Hitler also believed capitalism was a jewish plot to control the world so go figure.