Tragically, there are very few public toilets in Rome (hence the horrible odor you’ll encounter near alleyways and on the beautiful but isolated banks of the Tiber at Lungotevere). And many of the ones that are available are very nasty indeed.
A few Metro and train stations have them, usually requiring a €1 coin to enter. Some of these are clean-ish, particularly in Termini.
So what do the locals do?
I am told by Rome residents that by law, as part of the licensing conditions, every bar must allow members of the public to use the bathroom even if they don’t buy anything. In practice this can prove fraught, especially for tourists and visitors, as many bars have the key to the bar behind the counter and put up signs saying “clients only”; others tell you “the bathroom is out of order” (but it magically works again if you are a customer).
If that should happen, some locals have told me you should mention the cops, or pretend you have a medical condition in order to prey on their sympathies. My advice is, find another bar. If it’s a crowded, busy bar then just walk in, work out where the bathroom is, and brazen it out – my thinking being, what’s the worst they can do if you’ve already made it into the stall – shout at you as you leave?
If it’s a small/quiet place, then it’s polite to ask to use the bathroom: “posso usare il bagno?” (POSS-oh oo-ZAR-eh eel BAN-yo?) or just ask where the bathroom is: “dov’è il bagno?” (doh-VEH eel BAN-yo?), but if this makes you feel uncomfortable then buy a coffee, a bottle of water, or a pack of gum, then ask to use the bathroom and they’ll be perfectly happy to allow you.
If you’re really worried, though, there’s an app for Android that gives you a map of the city with the nearest bathrooms shown and details about them (Italian language only though).