All electronic devices become unsupported at some point. It's just a question of how long. Though of course there is no law that says you need to replace it once the updates stop - I usually use mine for a few years after that.
In the case of android phones how long you receive updates for depends on the manufacturer and the model. A Google Pixel 2 will receive at least 3 years of OS and security updates ("at least" because that's what they have promised, but anything more should not be counted on). Major manufacturers' flagships (Samsung, LG, Sony, HTC etc) will generally get 2 years. Cheaper models in their ranges often get less, as do phones from most lower-tier manufacturers. Budget phones from fly by night Chinese manufacturers will never get any sort of update or software support. Put simply, software support costs money to provide and so is factored into the price of the phone.
Of course this starts from when the phone is released, not from when you buy it, so if you buy a year old model you will get a year less support (also true with Apple, the difference being that an older Android phone will be cheaper, while Apple will keep the same price as the day it was launched until the day it is replaced - I always tell people not to buy iPhones in the summer for this reason, as they would be paying full price for an older phone with a year less support.
All mobile operating systems need to be built for the device, so you can't just install your own version after official support ends. In some cases it is possible to get further updates using "custom ROMs", i.e. where an enthusiast with the necessary knowledge has built a newer OS for that phone and released it for other people to use. This does require subverting some security measures in order to install it, and tends only to be available for popular models (i.e. flagships, i.e. more expensive models). The demand for this is much less than it used to be a few years ago, but this option does exist for those who are interested.