When you are far away from any masses, that means when the spacetime
is flat, and you measure the speed of a light ray that passes you, you
will always get the same value c.
Yes.
Now suppose you are in a static gravitational field at a fixed
position, and do the same. Why do you also measure the same value c?
The correct question is "why not"?
We're all sitting on a geodesic, and that's the same for everyone around. Everything that you might measure, and anything you might measure with, all share the same physics.
So when you're on the flatter spacetime then you're using a flat-ruler and flat-watch to measure the flat-light. And when you're on a curved spacetime then you're using a curve-ruler and curve-watch to measure curve-light. So, one would normally imagine that you'd get the same results.
This is the whole idea of relativity; your local conditions define physics and since measurements are physical, they're defined by your local conditions too. Everyone is subject to those conditions, so it all ends up looking the same in the end.