It comes down to the party's National Committee to approve who runs in advent of death or nomination withdrawal and before election - and it has happened....
In 1912, Vice President James Sherman, a Republican, was running for reelection when he died days before the next election. The Republican National Committee named Nicholas Murray Butler as the replacement.
In 1972. Vice Presidential nominee Thomas Eagleton, a US Senator, withdrew from the race after there were disclosures of medical issues. The Democratic National Committee named Sargent Shriver, the former Director of the Peace Corp, as the replacement.
As to post election, it's thought that the same obligation of nomination would exist and in the case of President, it would probably be the Vice. There has been one incident with a Democrat dying between the election and the swearing-in but he was on the wrong side of the winning equation so it was irrelevant. 1872 and Horace Geeley was his name.....