It's not always clear from the movie, but Apocalypse actually possesses a ton of mutant powers. Over the centuries, as he moved from body to body, he's kept the mutant abilities of all of his previous hosts. As such, there's no telling what powers he has that we've never seen before. This means the writers could merely pull out some new random power at will whenever they needed Apocalypse to do something useful.
In this case, what appears to happen is that Apocalypse is able to "predict" how Quicksilver is moving, and where he's going to step at some point in the future. He then uses one of his more commonly-seen powers to soften the earth under Quicksilver's feet, so that when Pietro steps on that spot in the ground, he gets stuck. From there he causes the earth to wrap around Pietro's foot and ankle to prevent him from getting free.
How he's able to do this could be from any number of mutant abilities; perhaps he has some very limited form of precognition, and can "see" a few seconds into the future; perhaps he has heightened perception, and was able to track Quicksilver even at high speeds. Since the filmmakers didn't bother to tell us, all we have left is speculation.