I have 20 Commodore 64 computers that still work (a couple need some minor repairs that I need to get back to). How long they last depends on several factors. First is the power supply. The original C64 brick power supply usually was filled with potting compound which held in the heat, eventually frying the 5V...
Best answer: I have 20 Commodore 64 computers that still work (a couple need some minor repairs that I need to get back to). How long they last depends on several factors. First is the power supply. The original C64 brick power supply usually was filled with potting compound which held in the heat, eventually frying the 5V voltage regulator and making it send 12 volts to the power rail, frying any or all of the chips on board. There is something called the "C64 Saver" which can be purchased or built yourself to protect the C64 from that overvoltage problem. Alternatively, using a different power supply that has built-in overvoltage protection will do the same thing. If a chip does get fried, there are a lot of service manuals and repair tutorials online for diagnosing and fixing it. (Note that while some chips are off-the-shelf components that are easy to find, original Commodore-specific chips are far more rare and difficult to find in working condition.)
Second is the issue of heat. All the big chips on the board put out some heat, and some get quite hot. Many C64s had a cardboard and foil interference shield wrapping the top of the board. While that helped minimize stray signal interference from getting in or out, it also acted to trap the heat among the chips so they couldn't properly cool, and that will eventually lead to failure. If your C64 has that cardboard shield, take it out and throw it away. Then invest in some small aluminum heat sinks that you can stick onto the chips with either thermal paste or thermal adhesive tape (some heat sinks have peel and stick thermal adhesive backers already on them). If you have a newer C64 that has the solid metal shield over the board with tabs bending down to make contact with the chips, that's fine to leave in place because it acts as a heat sink to pull heat away from the chips.
Third is the matter of static shock. If you build up a static charge, especially in the winter when everything is dry and your footwear shuffling across the carpet makes you build up static, then you sit down at the C64 and touch any of the ports or peripherals on it, you could send a significant static shock into the system that may not simply ground out. You could fry any of the chips on it, making it malfunction or quit working entirely.
Fourth is making wrong connections. The SID (sound chip) is very susceptible to getting fried by stray voltages. If you connect the audio output plug to the wrong socket on the monitor, it could send voltage back in that reaches the SID and takes it out. It also has a little-known audio input pin that can be used for running external audio through its filters for creative music enhancement. That audio input must be at unamplified levels only; if it's amplified, that will take out the chip.
Fifth is things like spills on the keyboard, which can screw up the keyboard contacts at best, or at worst get down to the circuit board and start shorting things out. If you ever spill any liquids on your C64, shut it off immediately and disconnect the power, and then take it completely apart for a thorough cleaning. There are tutorials on youtube and elsewhere for how to take apart and clean the keyboard (it's tedious).
Last, and something you really can't prevent is simple degradation of the microchip transistors over time. Voltage running through them for years and decomposition of the materials will make them deteriorate at the molecular level until they reach a point that signals can't go where they're supposed to go and the thing starts to malfunction or quit entirely. Like I described above, chips can be replaced as this happens, but there will come a point when chips can no longer be found and it just can't be repaired any more.
Beyond the C64 itself, the floppy drives can suffer the same issues of power surges and chips going bad. Plus the floppy disks themselves won't last forever as their magnetic surfaces degrade over time. Most of my floppies still work fine, but some brands are worse than others and have started to fail (there are two brands that the magnetic media loses its adhesion to the plastic film substrate and the disk drive head scrapes the stuff right off, destroying the disk and requiring a thorough alcohol cleaning of the drive head). Luckily, there is a great source of aftermarket devices in the continuing fan base of C64 computers (the "Commodore scene"), including things like SD card readers that emulate real floppy drives and use disk image files to emulate an entire floppy disk in one file. You can even use them to make disk images of your current real floppies so you always have backups that are usable if the floppies go bad. It also works for tape image files to emulate the datasette (tape drive), as well as cartridge image files to emulate cartridges. It's pretty cool what people continue to do with it.
But your question has more to do with old computers overall, not just Commodore 64 computers. Really, the same advice goes for all of them: take good care of them, be aware of things that can cause damage like shocks and power surges, learn how to repair them, and see what aftermarket support there is for them. It does seem like older computers lasted longer than newer ones, but that probably varies from one brand to the next and one individual device to the next. A C64 or other old computer could potentially still work in a hundred years, provided there are still display devices that can accept the video signals. But just remember that nothing lasts forever when it comes to electronics.