Boxes come in two basic types Metal and Plastic.
For general residential construction the choice is left up to the contractor and if the box is not exposed then it will most often be a plastic box. Exposed boxes like the ones found in unfinished basements and outside the home are made of metal this is to reduce the risk of breaking the box and then having exposed wires.
Whether the box will be for a light switch or an electrical outlet there are a few rules to follow when installing the box.
The first is off the ground height. The National Electric and Building Codes will set the minimum and maximum height from the ground that all boxes must be set. If there are reasons such as accessibility for handicapped these codes can be adjusted.
During inspection all electrical boxes, wires and connections must be visible or accessible to the inspector.
New Construction boxes are nailed to the stud prior to installing the drywall.
Most boxes will have a gauge or clip on them so when you rest it against the stud the front of the box will protrude far enough that drywall can be applied and the box will be flush with the outside surface.
Post Construction or Retrofit boxes may come with clips that allow them to be attached within the wall cavity. The use of these boxes is really a last resort and the only reason is to allow fast installs without damage to the drywall. If you are planning to use such a box you should really install the box against a stud and nail the box from the inside to the stud. It is not that much extra work and will make sure the box is secured properly and not able to be ripped from the wall.
Often in garages or basements you may find a box on a concrete or bare stud wall that is not protected. In this case you must use a Metal Box that is designed to accept Conduit. Conduit pipe will be used to protect the wire that is run to the box. When you install conduit and metal boxes you will need to use a conduit tube bender. An offset is necessary at the point of the box to make the attachment.
Boxes that are exposed to the outside must be weather tight and should be metal. There are PVC plastic boxes that are available but the danger with using a plastic box is impact destruction. Outside switches and outlets can be covered with a flip up cover or some switches are weather tight and exposed. When choosing any product for outdoor service you should follow recommendations of both electrical code and the manufacturer. A box that holds water even days after a rain will be a danger to anyone near or using it.
Some boxes will come pre made to accept more then one switch or outlet. The most common is 2 or three but 4 or more can be made. Plastic boxes are not the best choice for gang boxes unless they are pre made. Special attention must be made to ground the box and not just the switches when you use a metal gang box. If you find that you are installing more then 4 switches or outlets in one location you may want to rethink your circuit plans. Locating the switches within a sub circuit breaker box or separating the switches by 1 or more wall void spaces will reduce confusion.
There are situations where a connection between 2 sub circuits must be made this can happen during initial construction or when you are retrofitting a building. Where you can not make connections within an outlet or switch box you must use a junction box to protect your splice. For instance you are running more outlets in your garage to install a garage door opener or outlets in different areas. The wire to the garage is located in the attic craw space however gaining access to the installed boxes is not possible. Where you cut the wire to make a splice you will install a metal box and connect it to a stud. Make all connections with wire nuts and install a cover plate to protect the junction.
When installing special appliances there are other types of boxes that are only to be used for that situation. Follow manufacturers instructions for any such install. Ceiling Fan boxes are both the support and the connection point for your fan. They come in 2 types New and Retrofit Construction. Be very careful to follow instructions closely and do not use a standard wall box for a ceiling fan.
Some items like furnaces and water heaters should not make use of a box and should be hardwired. This means a direct wire from the circuit breaker into the appliance / furnace.
Electrical wiring can be a danger if it is not performed correctly. If you have any doubts in your ability and if you have not attempted such a project before in the guidance of a professional then you should contract with a licensed electrician.