A race is only about 3 or 4 hours long, so it's often possible for them to go before the race, then not have to go again until after. In addition, the inside of the car can get very hot -- 130 plus degrees Fahrenheit or more -- because of the car's exhaust, which runs just beneath the floor. Because of the heat and the effort they have to expend, they're often more at risk of becoming dehydrated during a race, so they often drink lots of fluids leading up to a race and have a water or energy drink in the car that they'll drink from during caution periods. But, ultimately, if a driver has to go to the bathroom during a race, they would simply relieve themselves in their suit. There's far too much money and prestige at risk to worry about something minor like having to go to the bathroom.
A driver typically has a much more serious problem with dehydration. The tracks get intensely hot (130 degrees plus, by some accounts), and the suits insulate the drivers quite efficiently. Most drivers report a weight loss of 5 to 10 pounds of fluids over the course of a race. Oh, and in those superinsulated suits, there is no specially adapted device for handling liquid waste. Nor do drivers wear adult diapers, just in case. If a driver has been ill, or has eaten something that has given him diarrhea, they will give him a ton of Immodium AD.