The best explanation I have heard: in the days of wooden desktops in schools (real wood; not simulated stuff), guys would either look for....or leave...marks in the desktop from the previous guy taking a test.
The implication was, if he pressed hard enough, the pen or pencil would leave an indentation...a 'gouge"....that could be read by the next guy.
If a guy did it deliberately for the guys who followed, it was giving them "the gouge."
That one makes the most sense to me.
The term fell in to such common usage that a few generations later, no one knows what it means....just like the teens today who have no idea that the clothing line "Aeropostale" means "Air Mail" in French (I told my neices and nephew; they thought I was full of it...unitl google translate proved me right. They'll believe the internet before their Uncle...).