I am posting this because the subject seemed to be of interest to mechanical restorers in another thread.
The first issue to deal with, is whether the key is the rectangular type, or the Woodruff (half-moon) type. Of course there are other types as well, such as gib-head and Scotch feather, but the solution is either obvious (in the case of gib-head) or the keys are too uncommon to worry about.
To diagnose whether a key is rectangular or Woodruff, begin by looking at its proportions. A Woodruff keyway is cut very deeply into the shaft, and the length of the key is equal to the diameter of the milling cutter that made the keyway. In practical terms that means if the key is longer than two-thirds of the shaft diameter, it must be a rectangular type (otherwise, the Woodruff keyway in the shaft could not have been cut in the first place: the boss and shaft of the cutter would have hit the surface of the shaft). Keys of less than two-thirds of shaft diameter, and exactly the same length as the keyway in the shaft, are usually Woodruff, because it is pointlessly difficult to cut the keyway that way otherwise. However there are curved keyways that are not Woodruff: they are shallow like a rectangular key, but the underside of the key is part of a circle. These keyways are easy to cut in the shaft, but the keys are weak (very little average keyway depth in the shaft), and most likely you will not encounter them in anything made by a reputable manufacturer. If you are not sure whether a key is Woodruff or rectangular, and you can't tell by looking at the keyway, you can find out by trying both methods of key removal.
If the shaft and/or key is corroded, the first step is to soak it in penetrant for at least a day. I have had success with Penetrene, but if you are confident WD40 or another oil-based penetrant will do the job, use it. Next you have to free the key in the shaft, so it can move.
If it is a rectangular key, that requires tapping it axially along the keyway, using a suitable punch against the end of the key. Be fairly gentle - the steel used for rectangular keys is not much harder than mild steel. Having moved the rectangular key, your next move depends on where it is located. If there are no obstructions nearby, and the keyway ends in a gradual run-out (depth decreases), just tap it along the keyway to the end, which will cause it to rise up the slope until it is out of the keyway. At the other extreme, if the key is right at the end of the shaft and there is a bearing right behind the other end of the key, this trick may help. Using a punch the same size as the end of the key, tap the end of the key upward by tilting the punch. When the far end of the key hits the inner ring of the bearing, it can't go any further and it will pivot at that end, lifting out of the keyway. The most difficult rectangular keys to remove are fortunately uncommon: these are fitted to an end-milled keyway, with rounded ends of the key to match the ends of the keyway. The technique was reasonably common long ago for sliding keys: that is, the hub of a gear slid along the key, and the rounded ends closely fitted to the rounded ends of the keyway ensured that the key did not slide with the gear: it was held stationary by the ends of the keyway. A normal keyway, which becomes progressively shallower at the ends because of the milling cutter radius, would have caused the key to lift which would jam the movement of the sliding gear. The only way I have found to remove these keys, is to grip them laterally with vicegrips or something similar, and tug them upward out of the keyway.
If it is a Woodruff key, tap vertically down on one end of the key - that is, toward the center of the shaft - until it moves down to the height of the shaft's surface. The other end of the key will then be standing above the shaft twice as far as it did in the first place. Tap on this raised end of the key with a punch, along the axis of the shaft. It will either slide up the slope at the other end of the keyway, or it will pivot on the top edge of the far end of the keyway (nearly always the latter, in my experience). Just keep tapping gently with the punch kept as low as possible on the key. If it is particularly recalcitrant, after it has risen a fair distance you can pluck it out with vicegrips.
Woodruff keys are commonly made from a somewhat harder steel than rectangular keys. There are exceptions to this: occasionally you'll find a hard rectangular key. I once had to use a stepped key in a very highly stressed application - driving the rear hub of a car, which was on a taper as well as having the rectangular key. The failure of the original key had damaged the shaft, which was irreplaceable, so we had to widen the shaft's keyway. Widening the hub keyway was too difficult (it would have required a slotting operation at an angle to the hub's axis) and that keyway wasn't damaged, so we decided on a stepped key. However the fit on the taper was not going to be good, which meant the key would face a tough job surviving, due to the stress concentration caused by the step. Our solution was to make the key out of high speed steel. It survived several years of my driving then some more years of my brother using the car for racing, so I guess it worked.