For hard disks, you almost always get warnings, from various diagnostic programs. When you see S.M.A.R.T. errors, or you have sudden slow downs and weird sounds… it’s probably time.
For SSD, I guess you replace them when your computer suddenly doesn’t want to boot 😀
So far I haven’t had an SSD fail, but I’ve heard there are no warnings, so I always have backup running in the background, to my NAS (which uses two hard disks in mirroring configuration, with some lights on the front panel that show drive health, and email notifications and everything, so hopefully it’s unlikely that one fails, nobody notices, and then the other fails as well).
More important data also gets encrypted and replicated to cloud storage, so I always have 3 copies (two local, one remote).