The Walt Whitman poem book was given to Walter White as a gift from his ex-partner, Gale Boetticher, who had an almost fan-like obsession with him. Gale was also a fan of Walt Whitman poems, and coincidentally (or maybe not so coincidentally, as this is a TV show), Walter White and Walt Whitman happen to share initials.
After Gale was killed, Hank was given his file to look over in an attempt to figure out if Gale was the mastermind, Heisenberg. Hank found a dedication to a W.W. in the lab notebook. Hank read this and pondered who this was in a conversation to Walt - "Woodrow Wilson? Willy Wonka?... Walter White?", he added jokingly, to which Walt replied, in a sarcastic tone, "You got me!".
Fast forward to today. Hank is in the bathroom and looks for something to read while he shits. He finds the Walt Whitman poem book (that Gale gave to Walt, which he stupidly put in the bathroom). On the front page, Hank finds a dedication from a person named G. B., which, along with the content of the dedication and the handwriting, Hank recognizes as being Gale Boetticher. This dedication is to "*another * W.W.". This triggers the conversation Hank had with Walt and especially the quote "You got me!" leading Hank to make the connection that Walt is Heisenberg!
You must also remember what Hank knows about this Heisenberg. From the fake Badger drug bust that Saul organized as one of his first jobs with Walt and Jesse, Hank was told that Badger's supplier, Heisenberg, was an old bald man, in his 50s, average height and weight. Obviously, this profile has to be in the back of Hank's head the whole time.
Now, it clicks!