There are a lot of generic answers here about spacecraft. I will try to answer the question specifically for Spirit and Opportunity.
90 sols was deemed sufficient to conduct the primary mission of the rovers, so the systems were designed and tested to assure full capability through the entire 90 sols.
The first thing expected to take a rover below full capability was dust on the solar panels. The dust deposition rate and impact on solar panels was well known from Mars Pathfinder, a 0.3% multiplicative power loss per sol, and was considered to be a global constant in normal weather conditions. It turns out it is global. So the solar panels were sized to support all of the driving, instrument and arm operations, communication, and thermal control required for full capability given about 3/4 power from the solar panels. I.e. they were oversized by a third, compared to the power they could deliver with no dust.
(I know someone will then want to ask why there were no mechanisms to remove dust from the panels. There are many answers on this site and other places to that question. Suffice it to say that oversizing the panels by a third was far and away the cheapest and most reliable way to meet the 90-sol lifetime requirement. No money could be spent to go beyond the contractual requirements.)
We can see that even with the expected dust deposition, the rover would not just up and die at 90 sols. Its capability would only start to be reduced below "full". You could keep going for a long time, continuing to reduce the operations until the solar panels got so covered in dust that the rover could no longer communicate or maintain thermal control. Furthermore the power required for full capability was conservatively estimated during the design process, and the rover actually required less than those estimates for "full". As the rover was operated, we got smarter about how to conserve power, and could make each watt-hour drive that much further or send back that much more data.
Still, that 0.3% per sol is relentless. You can't go forever. Before we launched, I predicted that the rovers would each last for at least nine months before succumbing. They would be down to 44% power on the panels, and even more loss due to the seasonal movement of Sun north and so less light on level panels. Other folk on the project thought I was nuts. They were thinking six months, tops. In any case, there was no way they could go indefinitely, even if they were parked on the sides of hills to try to point the panels more at the Sun.
So what happened? How did they keep going after nine months? For years?!
Luck.
Every once in a while, around the same time of the Martian year, the rovers would experience "cleaning events". For terminology, "dust factor" is a percentage of energy delivered by the solar panels compared to when they were shiny and new, taking into account tilt, the latitude of the Sun, and atmospheric opacity. When a cleaning event occurred, the dust factor would jump up suddenly by 10's of percents overnight! This could occur for a few nights, removing the majority of the dust on the panels. Here is a before and after picture of Opportunity from the 2014 cleaning events:
(click, and click again to embiggen)
Each time this happened, the rovers would get a new lease on life. We got cleaning events reliably every Martian year. Until one year we got none for Spirit. Spirit died shortly thereafter.
The other expected life-limiters on the rovers were the brushed DC electric motors, and the lithium-ion battery. In fact, one of the wheel drive motors went out on Spirit, about two Earth years into the mission. Spirit continued to limp along for four more years, dragging that wheel through the dirt. (Once resulting in a scientific discovery found in the trench left behind. In image below, you can see white silica in part of typical Spirit trench dug by stuck wheel.) Due to the failed wheel, Spirit became stuck and could not free itself. Its inability to position on the side of a hill when the Sun moved North again contributed to its loss when the cleaning events didn't return.
Opportunity also lost a motor, but it was a steering motor, and so had less impact on mobility. And Opportunity continued to see cleaning events each Martian year, up until it got hit by the giant global dust storm.
The motors were only tested to three times the 90 sols, simulating the operation and environmental temperature swings. And there were failures in some of the those tests, which resulted in some changes. So it is quite amazing that those motors lasted as long as they did, even with the two failures.
Though we were worried about the lifetime of the batteries in the rovers, they were remarkably reliable through their many years of operations, and lost very little of their capacity.
In general, electronics is not expected to degrade over time, so long as thermal control is maintained. You are only subject to random failures, which can occur. There were some failures in the flash memory on Opportunity, as it got older. Flash memory has a wear out mechanism, though we tend to not notice it since we don't use the same flash memory for a decade. Eventually the operations team gave up on the flash.
Bottom line, Mars cleaned off the solar panels most of the time, but in the end both rovers died because of dust. There were in fact two motor failures, but the rovers were able to keep going. The batteries held up way better than we expected. The electronics I would expect to keep working, though the flash memory failed on one of the rovers.
That's how the rovers lasted so long. Every spacecraft's story is different.