Sometimes when I want to take a sample of my beer I open the tap of the fermenter (in my case a plastic bucket) and let drip a little bit of beer (primary fermentation). Does this increase the chances of contamination? Is it safer to open the lid of the bucket and take the sample from above?
Using the spigot is undoubtedly much safer than opening the lid.
With the spigot on your bucket you're basically just creating a hole where the beer can flow out (and only out). There are some potential sources of contamination here. The small section of the spigot blocking the flow of liquid will rub against other pieces of the spigot and potential introduce contaminants that it comes in contact with when it is opened once it is closed again. That said, if you sanitize the bucket and run some sanitizing through the spigot this is probably minimized. Repeated samples may increase that risk more but unless you're taking samples very early in fermentation (when the amount of alcohol is still low) I doubt you'll realistically have a problem.
When you open the lid the situation is much worse. While you may have some things working in your favor (a layer of CO2 over the beer, a protective layer of krausen, etc.) you are still opening the fermenter to airborne contaminants (pieces of dust, falling bacteria, your breath, etc.) all of which pose a small threat to your beer. On top of that, you then have to physically put something in your beer in order to take the sample. This is not only a risk of contamination but also of oxidation. Taking your sample quickly with a well-sanitized wine thief or baster/pipette may minimize your risk but is ultimately causing air exchange and allowing a greater surface area of new material to touch your beer.
All of that said, if you're careful, either way is probably fairly safe. Plenty of people do both all the time without any issue. But using the spigot is going to have fewer things that can accidentally go wrong and those are probably smaller risks (assuming equally stringent cleaning and sanitation practices for both methods).
Going to build on Denny Conn's comment above, by saying that I've found it bad practice to ferment in a bucket with a spigot at all. Cleaning and sanitizing the spigot, and all its parts, of my bottling bucket is a tricky task. The spigot comes apart in strange ways and has multiple places that bacteria can hide and sour a beer I have fermenting in it.
When transferring to a bucket post ferment, with a spigot for bottling, the beer will have a % alcohol that will prevent bacteria from spoiling things, especially with such a short contact time. But to dump wort and yeast into a bottling bucket is asking for trouble.
Suggest you use a spigot-less bucket to ferment, and only use one with a tap for bottling. Obviously, your spigot might be a different build than mine, and might be easier to sanitize, but most I've seen are not.