You scare or startle the dog, and then reinforce the desired behaviour. The dog soon learns what not to do, and what is the route to the reward. Ideally, you first teach the dog, via positive reinforcement, what the desired behaviour is, and only use punishment when you ask for that behaviour and don't get it. Anything else is unfair to the dog.
I'm not always popular on this board as I do use aversion on my dogs: I have very high-drive, prey-driven HPRs and sometimes they do need to know, clearly, what they must NOT do, for their own safety, the safety of livestock and wildlife, and the safety of any motorists who might be around should they hurtle across a road after a hare. They require free running for their fulfilment and sanity and apart from one playing field a fifteen minute drive away, there is nowhere locally where I can guarantee no livestock, rabbits, hares, muntjac, roe, pheasants, partridges etc. I still use far, far more positive reinforcement than anything else, but I have drawn some very clear lines.
I have two happy, affectionate dogs who are very confident out and about in the countryside (other dogs, horses, bikes, farm machinery up to and including combines - none of it phases them) or walking on pavements near traffic. They are great with guests, lovely with children, fine with the vet - and safe off-lead.