General Advice and Good Practices
Generally speaking, if you keep your device up to date, be it your PC or smartphone, or any other device with a webcam, this will mitigate the risk of being hacked. This is because updates ship with security patches making it harder to exploit old bugs.
Besides this most devices will also allow you to choose what programs can access your webcam and microphone making it harder for intruders to gain access to your device (PC Settings – Privacy – Camera).
It is also advised to not open or download unknown software or exes, or even click on random links that you might receive through emails or other social media. Most of these are key-loggers or other types of software that can even take over your webcam device.
You should also keep your home/office network safe and not join open networks because you are more vulnerable to attacks while connected to such networks. Unfortunately, all of this might not be enough to keep your webcam fully safe from being compromised.
Cutting access to your webcam and not having malicious software installed might make you think that you are immune and your webcam cannot be hacked but that is not true. In reality, as long as your device is connected to the internet and someone really wants access to your webcam, using advanced hacking tools they can get it.
The only real way of making sure that your webcam is not hacked is by simply unplugging said device from your PC. It might sound like a hassle to plug it in and out depending on use but if you manage your cables right, this hassle is worth the extra safety of knowing that nobody can ever access your webcam with malicious intent.
If there is nothing to access there is no way to get a live video feed from your device.
This becomes a little bit more complicated with devices with built-in webcams since those cannot be unplugged. In most cases, people resort to simply taping the webcam over with something. This might look very DIY but as long as the intruder cannot get a hold of your live video feed his attempts will have less power.
At the same time, most webcams and devices also have microphones and that piece of tape cannot help you much with that. In the end, the best-case scenario is if you have a dedicated webcam that can be physically unplugged from your PC/laptop.
Privacy at Risk
The only conclusion most people make when trying to figure out how to keep their webcam safe is how little control they have over their privacy. Besides the very specific situation where you can unplug your webcam altogether and you have no other camera/microphone device on your PC there is no situation where you are safe.
Taping your webcam is a band aid solution that can help you at least mentally feel more secure but cutting off access to in-built microphones on devices like laptops and smartphones is impossible since they are so tightly integrated into everything.
In the end, by keeping your devices up to date, not clicking on shady links or software, and keeping your network safe, you are doing most of what you can practically do to keep your webcam safe. This is the unfortunate reality of the 21-st century.