We must look to Robert Anson Heinlein for the origins of this word, which he invented for his science-fantasy book Stranger in a Strange Land in 1961. In this, Valentine Michael Smith, a human being raised on Mars, returns to Earth with psi powers given him by the Martians and is transformed into a messiah.
Grok is a word borrowed from Martian (and you won’t see that written very often) in which it literally meant to drink. However, in its figurative sense, to grok is to gain an instant deep spiritual understanding of something or to establish a rapport with somebody.
Smith had been aware of the visit by the doctors but he had grokked at once that their intentions were benign; it was not necessary for the major part of him to be jerked back from where he was.
The book became a cult classic despite its deeply flawed nature. Heinlein remarked self-deprecatingly about it that it was incredible what some people would do for money; it was originally published in a brutally edited form and became available as originally written only in 1990.
The term went into the language, at first among countercultural types in California and among SF fans (there used to be lapel buttons around with the message “I grok Spock”), but was later taken up by computer geeks and scientists, among whom it has largely remained.
I recall well when I first grokked Newton's arguments giving the special properties of the inverse square law. I was so moved by the elegance of the constructions, I found myself wiping away tears.
Science, 20 Apr. 2001.