On a piano, the hammers strike the strings, causing them to vibrate. This vibration is what creates the sound. The strings are made of different materials, such as steel or nylon, and they are stretched over a frame. When the hammers hit the strings, they vibrate at different frequencies, which creates different pitches. The higher the pitch, the faster the string is vibrating.
On other types of keyboards, such as organs and synthesizers, the sound is created by electronic means. However, the basic principle is the same: a vibrating object creates sound waves that are then amplified and played through speakers.