If either nominee dies or withdraws before the Nov. 3 election, his party — @DNC or @GOP — has to designate the replacement. But whether there's enough time for that nominee to get on the ballot is … up to the states.
Given that 29 states have already begun mailing ballots to voters, this could be a big legal mess, tied up in state and federal courts. Then there's the Electoral College …
Some states prohibit electors from casting ballots for anyone other than the person who won that state’s popular vote. If those electors were to choose their party's new nominee (instead of the dead/withdrawn candidate), that could also … end up in the courts.
“The political parties need to clarify their rules after this election. Congress should consider legislation to resolve some of the remaining uncertainties.” — Richard H. Pildes of NYU Law. In 2016. https://www.washingtonpost.com/news/monkey-cage/wp/2016/09/14/what-happens-if-a-u-s-presidential-candidate-withdraws-or-dies-before-the-election-is-over