Most of the screws used to mount cabinets in a kitchen, bathroom and other areas are drywall screws. Drywall screw heads flare out and mimic the shape of a bugle. Don’t be tempted to use drywall nails as a substitute for drywall screws to mount cabinets because the nails don't have the twisted threading that screws have to help them grip the wall upon installation. Without that grip, nails could pull away from the wall under the weight of the cabinets they're supporting.
Home Depot and the This Old House home improvement website recommend using 2 1/2-inch drywall screws and finishing washers of a matching size to mount cabinets to a wall. Finishing washers also are known as trim washers, and they have a curved edge that fits snugly around the head of a drywall screw. Avoid using a shorter screw to mount cabinets to a wall because the depth at which a screw penetrates a surface enhances its grip. All screws generally have a better grip than nails, but they can still pull away from a wall under the weight of cabinets if they're too short.
Find and mark the wall studs where you're installing cabinets before you begin mounting them. One of the easiest ways to locate studs is with an electronic stud finder. Cabinets need to be screwed in place through a stud, because the hollow sections between studs can't support their weight. Use 1 1/4-inch drywall screws to fasten side-by-side cabinets to each other after screwing them to the wall.
Mount cabinets that rest on a floor near a wall to the wall and to each other, using the same screw sizes recommended for other cabinets. However, the This Old House site also recommends using 3-inch drywall screws to secure floor cabinets to the floor. That might seem like overkill, but cabinets can become misaligned over time from daily use if they're not secured with enough screws.