If you need to do a #1, there is a very simple answer…just go. In the swim, the pee might actually provide your body with a brief warming sensation as it moves around inside your wetsuit which can be quite welcome in cold water. On the bike it’s a little more complicated. You can wait until you are far from a populated portion of the course and simply pull over to the side of the road, stop and discretely pee. There are also portable toilets at the aid stations where you can safely dismount and do your business in private. The technique I choose, on the other hand, is to simply pee while still moving on the bike. I wait until I reach a portion of the course that is slightly downhill so that I can coast. I, then, lean out over the left side of the saddle and let her rip. It is important to get your midsection far enough from the frame of the bike so as not to urinate on your water bottles. For the run, the same options as for the bike apply. Peeing while running is best but takes the most practice and some serious mind control. The good thing is that all the concentration required to convince your body that it is okay to pee while moving, with your clothes on, helps you to forget, however briefly, just how fatigued you are.
Number 2 is a bit trickier. The only sanitary way to perform this is to hold off as long until a portable toilet is available. They can be found at the aid stations and in transition. If the course is adjacent to a wooded area, you can always find a tree to hide behind and pop a squat.