One cause of this is when you press two keys at almost the same time, one of which is a movement key and the other opens a GUI. A well-known "poor-man's autorun" in Minecraft is to press the "walk forward" and "inventory" keys at the same time. You will begin walking — with the inventory screen open — and will continue walking after closing your inventory again, even though no key is held down.
Other input "collisions" may cause this as well, but as badp points out, the only real after-the-fact solution is to press the "stuck" key again. There is no way to prevent this kind of key-sticking, either, other than avoiding the simultaneous inputs which cause it.