Connecting to a switch:
Connecting to live power from a switch can be much more complex than for an outlet. Switch boxes, especially multi ganging switches can have an enormous amount of wires in them. It may be necessary to use a meter or tester to determine which wires are live.
You need to identify which of the black wires on the switch is the live wire.
If there are only 2 cables in the switch box, one of them is live and the other is going to the device that is being turned on and off. If the switch was wired according to proper electrical practices, the live wire is on the darker brass screw, however the odds are probably no greater than 60/40, that this is the case.
In some cases you can make an educated guess. If one cable enters the switch box from the bottom and the other from the top and the switch is controlling a light fixture the odds are pretty good that the top cable goes to the light fixture and the lower cable has the live power, but this isn't better than a 80/20 guess.
Figure 1 - Wires joined together with pigtail and with wire nut
Once you have determined which is the live black wire, remove the from the terminal and create a pigtail connection with the black wire that is running to the new receptacle as shown in Figure 1.
With two cables there will be a wire nut connecting the two white wires together. Remove the wire nut and add the stripped end of the white wire to the grouping. Twist them together to make a good mechanical joint and then replace the wire nut.
It may be necessary to use a larger wire nut in order to cap the three wires instead of the two that were previously joined.
If there are more than three cables in the switch box, there should be some wire nuts joining groups of wires as well. The wire nut that is connecting a number of black wires together and the wire nut that is connecting a number of white wires together are the groupings that you need to add the new receptacle black and white wires to.
Remove the wire nut, by twisting it counterclockwise. Add the stripped end of the wire going to the new receptacle to the grouping. Twist them together to make a good mechanical joint and then replace the wire nut. It may be necessary to use a larger wire nut in order to cap the the extra wire instead of the number that were previously joined together.
Connect the bare copper ground wire from the new receptacle to the ground wires that are already in the switch box in the manner previously described.
Mount the switch in the electrical box and replace the cover plate.
Connecting to a light fixture:
In a light fixture electrical box with 2 cables, one is bringing the live power to the electrical box and the other is going to the switch.
The light fixture should have colored pigtails, black for connection to the live wire, white neutral and either a green or bare copper wire for ground.
There will be a wire nut joining the live black wire to the black pigtail coming from the light fixture. Open this connection and add the black wire going to the new receptacle.
There will be a wire nut on the white wire that is from the same cable as the black wire above (the one connected to the black will be joined to another wire from the other cable, it may be white or black. Remove the wire nut and add the white wire from the new receptacle to this grouping.
Follow the mechanical connection instructions for adding wires to a grouping previously described under "Connecting to a switch".
There will also be a wire nut connecting the white wire from the light fixture to either a black or white wire headed for the switch. DO NOT open or connect to this joint.
If there are 3 or more cables in the electrical box, there should also be a grouping of black wires, including the black pigtail from the light fixture connected together with a wire nut, a grouping of two or more white wires (there may also be one black wire in this group) and the white pigtail form the light fixture connected with a wire nut to a black or white wire. Connect the wires from the new receptacle, black to the black grouping, white to the white grouping and leave the connection of the white fixture wire alone.
Join the ground wires together as previously described in "Connecting to a switch".
Note: Depending on the number of lamps in the light fixture there could be more than one black and white pigtail leaving the light fixture. The fact that there is more than one pigtail is of no consequence, the pigtails should end up in the same connectors and should be treated as if they were only one wire.