Being polite in Italian requires some basic vocabulary. These should be the first words out of your mouth in various situations:
Buongiorno
Whenever you set foot in a cafe, shop, restaurant or basically anywhere this is the first thing you should say, this works from morning till just after lunchtime and it’s a formal and informal greeting so you can say it to everybody
Buonasera
Same as the above, but used after lunchtime right through the evening
Buonanotte
Only to be used just before you go to bed (for example it's not normally used when leaving a restaurant after dinner)
Buon pomeriggio
Don’t say this ever! Even though ‘good afternoon’ is used in English speaking countries, it is never used in Italy except by presenters on tv
Buona giornata
When you’re leaving somewhere, from morning till just after lunchtime. The equivalent of ‘have a nice day’
Buona serata
Same as the above, but used after lunchtime right through the evening
Arrivederci
This is the safe option for ‘goodbye’ instead of ciao when you’re leaving. Always to be used when leaving a cafe, shop or restaurant etc.
Ciao
Used both for when you’re arriving and when you’re leaving. It should only really be used for friends and acquaintances, but if you bump into the same person a few times then it’s acceptable
Salve
This is your alternative for just about everything above if you forget any of them and can be used throughout the day. This is the one I prefer and it is very common all over Italy