How do robins know when worms are in the ground near it?
They see them.
Robins don't typically go after worms that are under the soil. Robins are not diggers.
But worms don't actually spend all their time below ground. They eat rotting vegetation which is most plentiful right at the surface of the soil and underneath the thatch from the grass. Sharp-eyed Robins can see the worms moving around underneath the thatch, and they snag them.
Earthworms are actually very interesting creatures. They are not native to North America but were brought here from Europe a few hundred years ago.