Some public bathrooms (including in train stations) cost 50 cents to 1 euro. Cafes' bathrooms are free if you're a customer.
If you really need a toilet and a cafe is the closest option, order a 1.50€ espresso and use the bathroom guilt-free.
Department stores have free bathrooms. McDonald's and Starbucks often require a customer code.