I believe the bottom ones are guardrails. They are present on bridges and viaducts. If the train derails they prevent it from falling into water, onto ground that's really far down, or onto populated areas. I don't know what the top things are
The yellow things have long stakes or spikes that go into the ground. They're to prevent movement of the sleepers. There's probably an increased risk of lateral movement at that point due to the curvature and cant, so they mitigate any risk of the track being out of gauge.
Not sure what the square metal boxes fixed to every fifth sleeper are for. I think this is what the OP is referring to.