It is unfortunately a complicated situation that often doesn't have a perfect solution. Without trying to cover every possibl nuance, I'll just say that many end-stage patients have pain so refractory that sufficient doses of pain medication are effectively lethal. This is usually offered, but obviously not everyone accepts.
The truly sad cases are those in which the patient no longer has decision-making capacity and it falls on a family member to make such decisions. When you aren't the one experiencing the pain, it's much harder to weigh that decision rationally. This is why so many providers push patients to establish legal documentation of their end-of-life wishes.