
As we expected, Avengers: Endgame involved time travel. In fact, most of the movie had characters travelling across different timelines to collect the Infinity Stones and undo the snap.
The (remaining) Avengers had made peace with the fate of the universe when Scott Lang got out of the and informed them about the Quantum Realm and how it can be harnessed to play with the rules of time.
Initially reluctant, Tony Stark decides to help them, and that’s good as it turns out Hulk/Bruce Banner’s knowledge will not be enough to safely travel through time and retrieve the Infinity Stones.
Scott is disappointed to know that time does not work like in Back to the Future. Bruce says one cannot just go back in time and change their past in order to change the future. Because the “future” has already become one’s past (in that it happened before).
The mission, he explains, is just to bring back the Infinity Stones and that is all. No need to tinker with past too much or there will be different timelines in which events had transpired differently.
So, if they go and kill baby Thanos, it would do zilch about the Thanos they fought and who halved the universe’s population since the died-as infant Thanos would be a different one, with his own timeline. In that timeline, probably the Avengers will not be needed at all as Loki would not get the Chitauri army to invade the earth (just a thought). This is explained further by the Ancient One to Banner himself, that removing the Stones from their time would cause the timelines to divide into several timelines.
And this is why after undoing the snap, Captain America goes back to put the stones back in their own timelines, so that the timeline would remain intact.
So, this was just how time travel works in the MCU.