the reason it occurs is that the version of lwjgl packaged with minecraft is woefully out of date (jeb_ has attempted to fix this in recent snapshots to no avail) One published bug is that it will not cancel a keyboard command when a click has been registered within a short time period after the key is depressed. this is most commonly found when moving items out of an inventory (alot of holding shift and rapid clicking). With enough speed (especially on a linux machine) you can get the directional keys or jump stuck in the depressed position (cost me many a diamond when working around lava).
A simple fix is to manually update the lwjgl packaged with minecraft. Or to install optifine and enable the "smooth input" option, the first fixing the glitch behind it, the second using a cheap trick (1ms delay for keyboard and click actions) to make it much more difficult to cause in the first place.