Have an observer in front of your army when engaging the enemy or crossing the map.
When you notice your opponent has them cluttered around the map, at least as Protoss it's quite safe to send an observer around. Once you spot one, just send a single stalker over. Widow mines have very limited range.
When your opponent has them in his army composition and you see them burrowing as you engage, it might be too late too run all of your units away. At this point, I think it's always best to send a single unit right towards the widow mines, as they always attack the nearest unit.
This also works great when you don't have detection yet and a widow mine is inside your mineral line.
If you're fast enough, split your army.
As Zerg, its much harder to avoid taking too much damage from widow mines and you have to be much more cautious with your overseers. Two hits from a widow mine, and your overseer is gone.