Recently I had the pleasure of visiting the Castle at Santa Maria da Feira in Portugal. It's a beautiful castle! Here are some pictures I took of the Castle itself, the Courtyard, and the space on the Roof, for anyone interested.
So like any castle, this one had walls, and I noticed that while some portions were pretty wide, most of the time the walls were really narrow - sometimes really really narrow.
There's a portion at the back of the castle made of several levels, the lowest of which is a ~10ft trench. The walls over this trench were stupidly narrow, I had to walk sideways too keep from going over this ~15ft drop.
That got me wondering: Me, I was taking my time and being extra careful on these walls - but amidst the commotion of a siege, people in leather/chainmail would be running back and forth, possibly under fire, pushing past people going the other way...how the hell did they keep from falling off these narrow walls and injuring themselves?
There was usually a wooden fence behind the ramparts. These didn't survive the centuries and are no longer visible. Some other castles do have a wooden walls behind the ramparts which are still present.
[Maybe they didn't want the guards leaning against the railing it all day.] (https://www.youtube.com/watch?v=9bSZXucTH4A)
Building on what u/bartieparty has said, many castles had large wooden structures sat on top of the walls. (I think it was called a battice)
This is what it looks like, or you can see one in this scene here. As well as providing murder holes and making the walls harder to scale, it allows a lot of people to shove each over around without falling off.