Most people are thrilled to get back to shore at the end of their swim, but it consistently proves to be one of the most difficult aspects of ocean swimming for triathletes to master. First, know that because it is the end of the swim, you'll likely be tired, so exiting efficiently is most important. As opposed to the swim entry, where you're going against the grain of the water's movement to shore, during the exit you can utilize the energy and movement of the waves to aid your efforts. Instead of breathing directly to the side per usual, breathe back into your shoulder so that you can get a peek at the waves behind you with each stroke. This will give you the visibility and spatial awareness necessary to gauge where the waves will be breaking and how to act accordingly.
As for when to act, act early. If you see that a wave will be breaking within 10 feet of you, be sure to dive down and in the same direction as the shore a few seconds earlier than you may naturally think. By the time the wave or white wash gets close to you, all of that energy will push you around and it will be too late to avoid the turbulence. You'll typically only be underwater for a few seconds, so utilizing a long, sighing exhale will help keep your body relaxed until you surface. Seamlessly go into your freestyle stroke again to maintain progress into shore.
Perhaps the best piece of advice is to swim all the way into shore until your hands are hitting the sand multiple strokes in a row, pop up and then run with high knees out of the water and onto the beach. The less time spent in waist and chest deep water where the waves are breaking and water is pulling you back out, the better.