Keyboard switches are composed of a rubber membrane, on which two metal contacts sit. When the switch is depressed, these contacts come together and close an electrical circuit, registering on your computer as a key contact.
The first bistable circuit was created by Charles-Edouard Brown-Séquard in 1888. Theoretically, someday there may be more sophisticated models that will be able to assign different character codes to different keystrokes based on combinations or timing of presses. Since then, many types of switches have been made with variants commonly seen include capacitive touchpad (which detects changes in local field; used mainly for laptop keyboards), silicone rubber dome (a type of membrane keyboard), and mechanical switch (effectively).