The worst thing that can happen to the keyboard is water damage, such as spilled coffee or juice. This type of mishap may not ruin the keyboard, but it depends on the type of keyboard, whether it’s mechanical or magnetic.
The first thing you should do after spilling liquid into the keyboard is to detach it. If you’re using a USB keyboard, simply unplug it. The older PS/2 connector keyboards shouldn’t be disconnected without turning off the PC first. Otherwise, by disconnecting the PS/2 connector, you may accidentally damage the keyboard connection circuitry or the PC’s motherboard. That’s a bad thing.
After removing the keyboard, try to give it a bath. Fill a washtub with cold, soapy water. Immerse the keyboard in the water and give it a few good shakes. Then let the keyboard drain on a towel. Let it sit overnight or until it’s completely dry — typically, overnight.
The next day, after the keyboard is dry, attempt to plug it back into the PC and see whether it can be used. Often, the keyboard recovers just fine. If not, replacement keyboards are cheap.
Of course, none of this applies to a laptop keyboard. If you spill liquid in a laptop keyboard, you got liquid in the whole dang thing. Off to the laptop repair shop or laptop replacement store you go!
Mechanical keyboards are more expensive than their magnetic and membrane cousins. They do, however, stand a greater chance of being cleaned by using the water-immersion method.
Cheaper keyboards use magnets and membranes to make the keys connect. In that case, immersing the keyboard to clean it may render the thing useless. Of course, it was probably rendered useless immediately after you spilled coffee into it, so the point is moot.