It derives from the prefix kilo, used in the metric system to mean a unit of one thousand, as in kilogram, kilometer, kilowatt, etc. When abbreviated it comes out as k, e.g. 5 kg, 5 km, 5 kw.
In computers K usually means 2^10 (two to the tenth power), or 1,024. That's close enough to 1,000 that most people just think of it as 1,000 -- and anyway, there's overhead used for system purposes so that you never really get 1,024 bytes of storage from 1,024 bytes of memory -- the 1,000 figure is pretty close.
Until recently M used to also mean 1,000, from Mill (also meaning 1,000, but in the metric system milo means 1/1000). But that got confusing because it's also an abbreviation for million. So today $1M usually means a million dollars, and $1B means a billion (a thousand million in the US at least, but it used to be a million million in the UK). Kind of confusing.