To expand on that, what the batsman is doing is effectively marking wherever that particular stump is, just right at his feet. That way when he gets into his stance he knows exactly where the stumps are behind him. He doesn't need to look back at the stumps wondering exactly where he is. It also lets him stand in pretty much the same position ball after ball.
The batsman comes to the wicket, looks at the stumps and then places it vertically along a particular line, either asking the standing umpire (who is in a perfect spot to see the relationship between where the bat is and the stumps) for his preferred line or where the bat is. So sometimes you'd hear "Leg stump, mate," meaning that the bat is supposed to be vertically in line with leg stump. If the batsman wants a leg stump guard, he uses the spikes on his boot, a bail, or very occasionally even the toe of the bat itself to make a mark where the leg stump line is.
The most common, as mentioned before is middle and leg, meaning the bat, when held vertically, is on a line between the middle and leg stumps. You'd pretty much never see a batsman taking an off stump guard. Middle is tricky, but it happens from time to time. Middle and leg is the most common, and leg is second most common.
Some batsmen will start their innings on a particular guard before changing it as they settle. Some batsmen will take different guards to different bowlers. Sometimes due to pitch wear and tear, etc, the original mark made by the batsman is getting difficult to see, so they just make it clearer by more scratching.
Long post, but to be honest, the whole taking the guard thing mystified me for years before I realised I was actually doing it for a long time, just using my own judgement without an umpire (street/backyard cricket rarely has those, of course) and without the scratching.