Major spoilers ahead for Avengers: Endgame. After Thanos wiped out half of the earth's population with the snap of his fingers in Avengers: Infinity War, fans scrambled to theorize how the remaining Avengers might undo its life-altering consequences. Most of those theories boiled down to some form of time travel, and they were correct. In Endgame, the Avengers go back in time to collect all of the Infinity Stones before Thanos (Josh Brolin) gets to them, but it's not with total clarity or ease that that happens. So how does time travel work in Endgame? The answer is far from straightforward.
Most of the action in the film takes place five years after the big finger snap that wiped out half of the earth's population. It's only then that Ant-Man (Paul Rudd) happens to return to the present day, back in Hank Pym's van, after a rat walks on the keyboard that controls the pathway to the Quantum Realm. You can thank that rat, then, for bringing back all of the Avengers that disappeared at the end of Infinity War, basically.
So Ant-Man makes his way to the Avengers headquarters and proposes that they go back in time to collect each of the Infinity Stones — because they can't do it in the present time due to the unfortunate fact that Thanos had destroyed them after the events of Infinity War. As with any time travel endeavor, the Avengers have a clear set of rules that they must follow to successfully execute their Infinity Stones mission.
At first, Tony Stark (Robert Downey Jr.) deems time travel through the Quantum Realm too risky as some sort of biological issue would mess with the time travelers' molecules, but then he finds out that he can do something to manipulate some particles to allow for Quantum Realm time travel. That's about as vague as the scientific aspect of the film gets, which is pretty standard for the Science Bros, but at least Professor Hulk — that's Bruce Banner merged with the Hulk (Mark Ruffalo) — clears things up during the actual time traveling scenes.
As well as Ant-Man thinks he knows about the rules of time travel based on movies like Back to the Future and Hot Tub Time Machine, the way time travel works in Endgame doesn't allow for the Avengers to make changes in the past that might alter the future. The surviving Avengers aren't actually trying to go back in time to stop Thanos from collecting the stones or to alter some chain of events, but rather they need to simply go back in time (to various locations) to collect each Infinity Stone and bring it to the present moment, which is unchanged due to the Quantum Realm allowing them to travel through both space and time.
When Professor Hulk runs into the Ancient One (Tilda Swinton) at Doctor Strange's (Benedict Cumberbatch) lab, the oracle explains some of the time travel rules a little bit more clearly.
As the Ancient One explains, collecting each of the Infinity Stones doesn't alter the future from which the Avengers came, but rather it threatens to create alternate timelines in which the Infinity Stones were gone during times they were actually needed throughout the MCU's chronology. For example, when Captain America (Chris Evans), Iron Man, Ant-Man, and the Hulk try to intercept the Tesseract from Hydra agents, Loki steals it and gets away, which didn't happen in the original events that take place in the Battle of New York from 2012's The Avengers.
Ostensibly, the timeline that follows after Loki steals the Tesseract in Endgame continues to unfold while the Avengers return to the present, the year 2023, unless the Avengers return back to New York in 2012 before Loki steals the Tesseract to replace the stones — which they do.
The whole idea that Professor Hulk learns from the Ancient One, though, suggests that as long as the Avengers go back in time after defeating Thanos to return the Infinity Stones, they won't have an impact on the rest of the universe's timeline of events.
The time travel in Endgame is complicated by the fact that the Avengers need to use Pym particles to execute their plan, and those resources are running out. That's why Iron Man and Captain America time travel even further back in time, to 1970, where they can both steal the Tesseract from Howard Stark and more Pym particles from Hank himself. However, the timeline in which Loki steals the Tesseract and disappears is already set in motion, so even at the end of Endgame when Steve Rogers goes back to return the Tesseract, the Loki-timeline has likely caused some sort of catastrophic ripple throughout the universe that the Avengers don't even know about. Maybe that's what his Disney+ TV show is about.
It turns out that time travel isn't actually as easy as they make it seem in Hot Tub Time Machine, as Ant-Man is disappointed to discover. Us too, Scott. Us too.