During the last scenes of the movie ''The imitation game'',the breaking of the Enigma code is presented as the main cause that lead to the eventual defeat of Nazi Germany.Is it factually true?
Not quite the main cause; there's a caption at the end of the film: "Historians estimate that breaking Enigma shortened the war by more than two years, saving over 14 million lives."
The main source of the figure of two years appears to be Harry Hinsley, a historian who worked at Bletchley Park, who sets out his reasoning in the introduction to Codebreakers: The Inside Story of Bletchley Park titled "The influence of Ultra in the Second World War". Hinsley's argument is, very broadly, that without Ultra Rommel would have exploited his victory at Gazala in 1942, and U-boats would have had greater successes in the Atlantic. The former would delay or cancel the invasion of North Africa, the latter would limit the build-up of US forces in the UK, therefore Overlord would not be possible in 1944 and would have to be deferred until 1946. The number of lives saved is based on each year of the war in Europe costing seven million lives.
As Hinsley himself says, though, "Who can say what different strategies [the Western Allies] would have pursued? Would the Soviets have meanwhile defeated Germany, or Germany the Soviets, or would there have been stalemate on the eastern fronts? What would have been decided about the atom bomb? Not even counter-factual historians can answer such questions."
Other assessments tend not to be so specific, e.g. Williamson Murray, "ULTRA: Some Thoughts on its Impact on the Second World War", Air University Review: "The dissemination of that cryptographic intelligence to Allied commanders under the code name Ultra played a substantial and critical role in fighting the Germans and achieving an Allied victory" though he also notes that "In war, so many factors besides good intelligence impinge on the conduct of operations that it is difficult to single out any single battle or period in which Ultra was of decisive importance by itself."
Peter Calvocoressi, Top Secret Ultra: "The breaking of Enigma cyphers played a significant part in the second World War. [...] To say that it won the war or even that it won a particular battle would be a silly exaggeration as well as a gross oversimplification of how wars and battles go. But without doubt Ultra made a big difference, sometimes a vital one."
There's some more discussion in a [previous answer] (https://www.reddit.com/r/AskHistorians/comments/2pq5fa/the_imitation_game_ends_with_the_quote_historians/cmz3q4l) between /u/k1990 and /u/restricteddata on the general value of counterfactuals that may be of interest.