I had this exact situation happen a number of years ago when I was driving our '97 Ford Taurus (it had undergone a number of repairs at that point, including an engine overhaul, so it probably wasn't due to an OEM defect). My first reaction was to stomp on the brakes, but it was immediately clear that the engine was winning that battle; so, I quickly shifted into neutral and pulled into the turning lane. My wife was in the car with me and remarked that she didn't think she would have known what to do, especially to react that quickly.
Our current vehicles are a manual transmission Civic and a PHEV Pacifica. The Civic is easy—just press in the clutch—but the Pacifica would be more tricky; I guess I'd start with the brakes, then the shifter (which is just an input to the computer, of course), then hold down the power button, and then look for a place to ditch.