When you alter the past in the MCU, the past you knew doesn't cease to exist. Rather, you create a branching timeline forking away from the previous one, so you now have two timelines where you previously had one. This isn't directly stated in the film, but it is indicated by the following conversation between Bruce Banner and the Ancient One, and a branching timeline diagram conjured by the latter.
THE ANCIENT ONE: I'm sorry, I can't help you, Bruce. If I give up the Time Stone to help your Reality, I'm dooming my own.
BRUCE BANNER: With all due respect, I'm not sure the science really supports that.
THE ANCIENT ONE: The Infinity Stones create what you experience as the flow of time. Remove one stone and that flow splits. Now, this may benefit your reality, but my new one... not so much. In this new branched Reality, without our chief weapon against the forces of darkness, our world will be overrun.** Millions will suffer. So, tell me, Doctor, can your science prevent all that?
BRUCE BANNER: No, but we can erase it. Because once we are done with the stones, we can return each one to its own timeline at the moment it was taken. So, chronologically, in that reality, it never left.
Avengers: Endgame
So when the Avengers went back in time to borrow the Infinity Stones, they created a series of alternate timelines where the Snap never happened, because the Stones weren't present in these alternate timelines in 2018. Each of these alternate timelines had its own version of Thanos, and it was one of the alternate versions of Thanos who travelled forward from 2014 to 2023 and died there in the film.
The events of Avengers: Infinity War, including the Snap, still happened, but they happened in a timeline which was now running in parallel to all these other timelines. A parallel universe, in effect. And when Captain America returned the borrowed Infinity Stones to the times and places they were taken from at the end of Avengers: Endgame, this erased the alternate timelines that were created by the Stones being borrowed in the first place (as per the Ancient One's branching timeline diagram in the video clip above), leaving only the timeline where the Snap occurred.