Apple does a much better job in keeping devices alive for longer than Android OEMs in general. Having full control over hardware and software and focusing on few devices helps there.
In Android land, this is unfortunately different. Longevity stands and falls (mostly the latter) with the (lack of) support of the chipset vendors such as Qualcomm and Mediatek. If they stop supporting a chipset, all devices built on them can not be properly supported anymore.
Just think of all of the unreported and unfixed hardware and software vulnerabilities in proprietary components. It is impossible to maintain a device without the chipset vendor's involvement. Apple, on the other hand, is its own chipset vendor.
The Android supply chain needs a drastic make-over to come anywhere near that.