I could only speculate (not having read much about worms), but it seems that there are many ways to tell (without sight, that is).
(1) The water they are fleeing is flowing (percolating) downward through the soil and filling up pore spaces. The water table rises upwards. They need only move away from the rising subsurface flood. Eventually, the hit surface.
(2) They can move against the flow of water (go up against the downward flowing water).
(3) They can move toward the looser soil (deeper soil tends to be more tightly packed).
(4) Following a temperature gradient?
(5) Following an air concentration gradient?
(6) Limited memory?
(7) Luck? (i.e., you just don't see the poor chaps that wriggle downward instead of upwards)