TL;DR If you mount the shelf supports directly to studs, do that. Just make sure to use long screws - at least 1" into the studs.
There are two typical ways to attach a shelf (or heavy mirror or whatever) to a wall with screws:
Anchors are the "plastic things". They come in various sizes/strengths - 30 lb. rating is typical. The way you normally use them is with a hollow wall. In other words, you don't use them if you are mounting over a stud. You drill a hole - or normally I just poke a hole with a screwdriver if the wall is made of drywall - in the wall slightly smaller than the anchor. Then push the anchor in, narrow end first, so that the wide end is flush with the wall surface. Place the shelf support over the anchor and drive the supplied screw through the support and into the anchor. The anchor will expand slightly, hopefully providing a tight grip on the drywall.
A variant is a toggle bolt, where a part goes through the wall, flips/expands and is pulled to the inside of the wall when you put in the screw from the outside. I prefer toggle bolts as they are a bit stronger than typical anchors.
You actually should not use an anchor (and impossible to use a toggle bolt) if you are mounting right over a stud. If you use a power drill then you could make enough of a hole in the stud for the anchor, but it really won't hold very well and it is not designed to be used that way.
Screws Into Studs
The gold standard is long screws into studs. With wood studs (metal is also possible but a little different), you typically drill a pilot hole through drywall and into the stud. Then hold the shelf support over the hole and drive a long screw through the support into the stud. Typically I would use 2" - 3" screws. You don't want to use screws that are too long in case there are wires running through the middle of the studs, but you do want to get around an inch or so into the stud, plus 1/2" for drywall plus ???? for the shelf support - so 2" is typical.
With screws into 2 studs, a shelf or cabinet can support a LOT of weight. But if you can only get into 1 stud due to factors beyond your control, then you can either:
- Use screws into one stud and use anchors for the other support(s). This will stabilize the shelf quite a bit compared to anchors alone.
- Mount a strip of wood horizontally into 2 studs and then use wood screws to attach the shelf supports to the wood. This will also normally be quite strong.