In my experience it happens for one of these reasons:
Your life changes in a way the friend can't handle. They desperately want a child and you get pregnant is a common one. They don't want to be petty or feel as they do but they just can't bear it.
Or you don't realise you've been draining them/making too many demands on them. If you've been through a tough time and using them as a sounding board or support system for a long time, they may suddenly feel they can't handle it.
Or they discover a side to you they just can't tolerate. I must admit I have ghosted a few friends because of this. How do you tell someone you can no longer stand them because they've revealed themselves to be something you just can't tolerate (racist or classist or a genuine narcissist? )
Or, they just weren't that into you. They didn't realise you were fostering a friendship. They just bumbled along meeting up because you suggested it, or being friendly at running club etc but really without any intention of getting close. Once they realise you're investing more in the friendship than they intended.
Or you served your purpose. Unwittingly, you were being used as an emotional prop or free childcare service by a selfish chancer and as soon as you meekly started making demands on the friendship in return, you got ditched.
All of them hurt, but long term I think there's no point in chasing a friendship that has been broken off. I've been on the receiving end of it and it hurts but I've also done it a few times - once for my own sanity and twice because I felt utterly drained by the people I'd befriended, both of whom had no job or children, were independently wealthy and had zero understanding of the demands of running a job, a home, a family.