Why does a printer keep going offline on a Mac?

My printer keeps showing as offline on my Mac, even though it’s switched on and connected to Wi-Fi. A restart sometimes helps, but the problem comes back later. How can I tell whether the issue is with the printer, the network or the Mac?