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?
There is a belief among many Rome residents that by law, as part of the licensing conditions, every bar (i.e. café) must allow members of the public to use the bathroom even if they don’t buy anything.
This is not strictly true. What is true is that all bars (and premises above a certain size providing certain types of catering) must provide a bathroom to their customers. While it is usually forgiven if you walk in off the street and use a bathroom without paying, in practice this can prove fraught, especially for tourists and visitors. It’s probably polite to become a customer by buying an espresso for €1 or less, or even a pack of gum.
What bar owners are not allowed to do is a) charge you to use the bathroom, or b) to claim that the bathroom is out of order. In the latter case such a claim renders their premises in breach of operative norms and thus they could risk being shut down and fined – and potential customers are within their rights to report them. It is from this that the erroneous belief that you can get the vigili (local police) to force them to let you use their bathroom comes from.
With all that in mind, if you’re in need of relief, find a bar, buy a coffee, a bottle of water, or a pack of gum, then 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?).
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), but public conveniences – when working – can often resemble the ninth circle of Dante’s Hell. Better to go for a pack of gum and a clean experience.