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.
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 eventually taken up by computer geeks, among whom it has largely remained.