How do teachers ensure that students stay on task when using their devices?
Keeping children on task for learning is one of any teachers key roles. It's what teachers do. Distractions to learning are not limited to students working on a device. Teachers will use a range of management strategies tailored for the student, and will take into consideration their age, and task that has been set.
Teachers often note that students are more engaged (and therefore less likely to be distracted) when they are using their devices. Why this would be is unclear, but for some children it will be related to the ease with which they can complete the task, e.g. some children find it easier to write on a Google doc than with pen and paper - these children are less likely to be distracted while working on a device.