It makes perfect sense that Luke would know how to use a grapnel, and it’s thanks to his Uncle Owen.
It’s established in the film that he’s a moisture farmer, an occupation devoted to gathering water from the harsh environment of Tatooine. However, the devices he directs Luke to service–presumably as part of his regular chores–are called “vaporators”. Now, for collecting water from the Dune Sea, that sounds like exactly the wrong sort of gadget; you’d want condensers, catch basins, and the like to collect dew.
This conundrum can be resolved if we note just where Owen says the vaporators are, though; they’re on the South Range. “Range” can mean a number of different things, geographically, some of which are contradictory. However, context suggests that in this case, it’s a reference to a mountain range (or at least, the largish, rugged rock formations that pass for mountains on Tatooine). This is because the vaporators are used to collect water that is deposited as frost where elevation, wind, and cold desert nights combine to chill depressions high up in the rocks (similar to the way ancient Egyptians harvested ice). They rove over the rocks, heating them to vaporize the frost for collection. They tend to break down in awkward places, so in the course of his chores, Luke often has to engage in a bit of rock climbing to get at them–hence his facility with a grapnel.
As to why the stormtrooper had one…think about those vertical shafts all over the place and put the pieces together. The shafts, grapnels, and lines enable rapid deployment, enabling troopers to hook on and rappel down to lower levels quickly in the event of an emergency that cut off normal routes. He may well have had a power ascender in his kit, too, but Luke probably didn’t recognize it.