why and how do I use a capacitor in a switch circuit？
The capacitor works like a RC filter to eliminate high frequency bounce when the switch is flipped.

For your other circuit, you should connect the cap from the signal pin to ground.