Is buying a ticket enough? Do I have to stamp ("convalidare") it? What if all the stamping machines are broken? What if I didn't have the time to stamp my ticket?
It depends. If your ticket comes with a seat reservation ("PRENOTAZIONE POSTO") then you do not need to cancel your ticket: just board the train.
Shorter range tickets with no seat reservation, on the other hand, must be canceled. Your ticket probably reads "DA CONVALIDARE" somewhere at the top. Trenitalia personnel typically doesn't cut tourists much slack and if "busted" you will be forced to buy a ticket on board with a surcharge. I've seen plenty of tourists fall for this on the Milan-Bergamo line and it saddens me.
If this happens, keep in mind that you're not being fined. The ticket you bought is still valid; you're just buying a new one. You can use that ticket for your return trip (just make sure you do mark it next time!)
Also, try and be honest and avoid getting into arguments. Ticket checkers have no qualms with waking people up and locking yourself up in the bathroom does not really work (it's no excuse for not slipping the ticket below the door). They can also actually fine you for quite a lot more money than just buying a new ticket with the surcharge (although I've never witnessed things degenerate to this level), followed by forcing you to step off the train wherever it stops next. Yeah, not fun.
When in doubt, stamp the ticket. If it's a paper ticket, use any of the yellow stamping machines typically found at the station entrance (by ticket sales) or at the lower end of the stairways up to the tracks. If it's a magnetic ticket (?) then use the blue machines instead.
If all the stamping machines should be broken, or if you just didn't have the time to stamp your ticket, or if you are iffy about the quality of the stamping (maybe it didn't print very well?) what Trenitalia and Trenord personnel have told me to do is to board the train at the head and tell the ticket collector when (if) he comes. He should sign off the ticket with no surcharge. I don't know if this is something official or just an informal convention, but I know it's worked for me in the past.