The Doctor (not the Strange one) once said, "People assume that time is a strict progression of cause to effect, but actually from a non-linear, non-subjective viewpoint, it's more like a big ball of wibbly-wobbly, timey-wimey... stuff." Basically, time and time travel is messy and, for sake of fiction, it requires a little bit of hand waving and at least some suspension of disbelief. While the rules of time travel vary from franchise to franchise, in good story telling, there are always some rules and the Marvel Cinematic Universe is no exception.
For those of you who haven't seen Avengers Endgame yet, there are spoilers ahead but for those who have seen Endgame, you may have some questions. Even within the movie, there were more than a few questions as to just how time travel worked and how it could be used to fix the mess left behind at the conclusion of Infinity War without creating another, potentially worse mess. Fortunately, between Bruce Banner, Tony Stark and a delightful cameo from Tilda Swinton's the Ancient One, the rules of time travel within the MCU were explained, though not without first calling out the way in which time travel has been handled in a number of sci-fi movies and shows (we're looking at you Back to the Future!)
To understand time travel in the MCU, there are three important rules to keep in mind:
You cannot change the past so no killing baby
This one is a biggie and kinda crucial to the whole movie - so much so that Rhodey even asks why they don't just go back and kill Thanos while he was still a baby. For starters, that would be awful, both in universe and for us as viewers. Much like the popular fan theory floating about social media that Ant-Man would shrink down, go somewhere unmentionable and expand, we really don't need to see Captain America stepping on a baby's neck (even if that baby would eventually grow up to wipe out half of the universe's population!) Aside from potentially traumatizing the younger viewers, Dr. Banner explains that this wouldn't work. Changing the past doesn't fix the present that our heroes are currently living in.
The Infinity Stones can bring everyone back but not by changing the past.
This rule isn't explained quite so directly as the conversation between Rhodey and Banner about offing baby Thanos so some viewers were a little confused on this point. At the start of Endgame, the Avengers track down Thanos in hopes of retrieving the stones to bring everyone back. Unfortunately, Thanos had managed to use the stones to destroy them so no one could undo his work. This, along with the return of Scott Lang from the Quantum Realm, is what prompts the plans for the Time Heist. If the Avengers could bring intact Infinity Stones to the present, along with a new gauntlet built by Stark, someone else could snap and return everyone lost in Infinity War.
It's important to note that this wasn't undoing the snap or changing the past. The snap still happened and the five years that had passed since then still happened in the same manner. This means that the half of all living things brought back are now five years younger than the half left behind. This should be especially interesting to explore in the upcoming movie, Spider-Man Far From Home. Will any of the important friends Peter had have survived the snap and, if so, haven't they long since graduated and moved on to adulthood?
The Stones have to be returned to the exact moment they were taken.
Related to our first rule, it's important that the Infinity Stones are taken back to the time when they were scooped up. This gets explained by Dr. Banner and the Ancient One in a conversation that may be a little heavy on the exposition but does include a lovely visual of the Stones creating a divergent timeline. Banner assures the Ancient One that they can maintain a single timeline, so long as the Stones are returned to the moment they were taken. That, along with the knowledge that Doctor Strange had given up the Time Stone in the first place, convinces the Ancient One to give Banner the Stone. Once everyone had been brought back and past Thanos and his Chitauri army are turned to dust, Captain America was sent on one last mission to return the Stones to their prospective times and assure that the past played out properly.
There are still a few concerns raised in Endgame and how time travel affected certain characters. People pulled from their own time or whose past we see altered thanks to time travel shenanigans don't seem to create divergent timelines or even necessarily alter the past. We have a few really good examples of this.
Thanos and Nebula come to the present from the past and promptly end up dead. (Nebula was even killed by her present day self in a strange suicide/homicide.) The death of his past self in the present didn't keep Thanos from completing the snap in the first place nor did it seem to have any impact on the past at all. Nebula, despite killing her past self, can be seen leaving with the Guardians at the end of Endgame. How either of these characters could continue doing anything after having died in their own future isn't really explained and may just be one of those things we need to hand wave away.
On the other hand, we also have Gamora. She came forward in time along with Thanos and Nebula and ultimately ended up turning on Thanos. Following a comical moment in which she almost breaks Quill for touching her, we don't really see her again. It'll be interesting to see how this plays into the next volume of Guardians of the Galaxy. This also raises a concern, given that Gamora had been traded for the Soul Stone in Infinity War. There is a sad moment in Endgame where Bruce explains that he tried to bring Natasha back with the Infinity Stones; he couldn't, despite his best efforts. Even if Gamora is able to carry on in the present, will she eventually end up in the Soul Stone anyway or, like past Thanos and past Nebula's deaths, is she able to carry on living in the present without impacting how her past self was sacrificed to the Soul Stone?
Then there is also the case of Steve Rogers. At the conclusion of Endgame, we find out that Cap had stayed in the past after returning the Infinity Stones. Rather than come back to the present, he met up with Peggy Carter (presumably sometime after the events of Agent Carter) and the audience is treated to a tear-jerking moment when they finally get to share that dance. It turns out that the husband who Carter had previously mentioned but never named had been Steve all along. You might be wondering how this never showed up in any of the records but much like Doctor Strange's warning to Iron Man during the climactic final battle, it's safe to assume that, if anyone had told him before, events wouldn't have played out right.
Last, but certainly not least, we have the fan favorite trickster god, Loki. He didn't end up time traveling but rather, when Stark and Lang attempted to steal the Tesseract right after the battle of New York, Loki got ahold of it and spirited himself away to who knows where. Given the recent announcement of a Loki focused show set to premiere on Disney's new streaming service, Disney+ later this year, we can only assume that this will be how we get a series for one of the many characters that didn't see a revival in Endgame. I, for one, look forward to the moment when Loki realizes that he can create duplicates of himself via time travel and starts mixing them in with his clone illusions.
So, there you have it: MCU time travel in a tidy, little package that hopefully didn't create any rogue timelines or universe shattering paradoxes.