If you want to go all-out, buy a small isolated power supply - a 3W isolated power supply takes 5V in, and outputs 5V, costs ~US$20, and has 4 important connections, one pair takes in 5V, the other pair outputs 5V. These are usually rated for 2.5KV of isolation. The LED side of the optocoupler and all the sensors are powered from this power supply, and "grounded" to a different ground than the local one, and the phototransistor (or whatever output) is powered from the nice safe local 5V power supply and ground connections.
I brought up the isolation thing because equipment that draws large amounts of current can cause grounds to bounce up and down by a large amount. If this is larger than the logic threshold voltages, it will cause intermittent signals or ground loops- someone switches on a large motor, and you'll see a glitch - or worse.
Now if you don't have any heavy equipment and everything is well grounded/or non-conductive - you don't need isolation, and simple current limiting/ESD protection is all you need.