A cog is originally a notch on a gear, one of the small little teeth that hooks onto other gears and makes them turn.
It first enters the English language in the 13th century and has meant much the same thing ever since. Here, for instance, is a quote from Stephen Hawes The pastime of pleasure, written in 1509:
A great whele made by craftly Geometry, Wyth many cogges.
Wasn't English much prettier in 1509? At least I think so!
Tracing the word backwards through history, we find that in Middle English it was cogge (which you can see in that quote). It was almost certainly borrowed from a word in Old Norse (the language the Vikings spoke in Scandinavia), and it still exists in some variant today in Swedish and Norwegian as kugg (meaning "notch", basically). Before that, it came from a Proto-Germanic root kuggo, probably also meaning "cog" or "notch". Proto-Germanic is the language that is the ancestor to all Germanic languages (including English), but it was spoken long before people were actually writing down words, so all the roots (like kuggo) are only "best guesses" that linguists have made to what the language might have sounded like.
Before that it's very speculative, but it might even be traced back to a Proto-Indo-European root guga, meaning "hump" or "ball". Proto-Indo-European is, like Proto-Germanic, also an entirely reconstructed language, but this one is the ancestor to virtually all the languages spoken in Europe (including English, French, Spanish, Italian, Greek, Latin, Swedish, Norwegian, Irish, etc. etc.) as well as a bunch of languages spoken in Asia, including Hindi and Urdu. We know for sure that it existed (probably around 4000 B.C in modern day Ukraine), but we can only guess what it might have sounded like.
Anyway, as cog made its way through English, it acquired new meanings. In the 18th century, people were calling gears cog-wheels (as in "wheels surrounded with cogs"), and soon people were shortening that to just cogs. Therefore, in modern day English, cog can mean both "a gear" and "one of the teeth on the edge of a gear", so some confusion over the exact definition over the word is entirely understandable.