According to series creator DJ Nash, the devastating death in A Million Little Things was planned from the beginning. When it was announced that the ABC drama was ending after season 5, it was promised that A Million Little Things would wrap its run with a bit of heartache. The series finale delivered in that respect, as a major development involved an exploration of assisted suicide. Maggie is with the dying Gary, who has incurable lung cancer, and when the time comes, he passes away.
https://screenrant.com/million-little-things-season-5-ending-death-response/