Ideally, the decedent discussed the role with you beforehand, and you knowingly agreed to serve, but in any case you can usually decline when the time comes if for some reason you choose not to serve (perhaps you don't have enough time, perhaps you are unable to handle the duties, perhaps you simply don't want the headache ... it doesn't matter). However, someone will have to serve in the role, and the court will appoint someone if necessary.
If you know you are going to serve as an executor, there are a number of things you can do before the decedent passes away that will make your task significantly easier, in particular gathering information ahead of time with the estate owner's help rather than playing a detective game later (see In Advance for more information).
Once the estate owner passes away, you are entitled to act as executor of the estate if:
Actually, if the estate is required to go through probate, you will need to be officially appointed executor by the probate court even if you meet one of the other criteria as well. That being said, there are some things you can do even before being officially appointed, and if the estate is not required to go through probate, you never need an "official" appointment.
If no one exists who meets the default criteria to serve as executor (i.e., named in the will or in the default line of succession), or the applicable people refuse the responsibility, the court will appoint someone else (perhaps a professional, or perhaps someone who desires the responsibility but has no legal standing to demand it). In fact, the court is not always obligated to follow the desires of the will or the default succession in any case.
If you wish to serve as the executor but there are other people with higher priority than you, you can normally achieve your goal by submitting signed waivers of those other people, stating that they do not wish to serve (and even better, also stating that they'd like you to serve).
You can even serve as the executor without an official probate appointment if the estate is not required to go through probate: see Alternative to Probate.