If the candidate wins the election but is never sworn in and dies, who is chosen and how?
According to the National Archives:
"If a winning Presidential candidate dies or becomes incapacitated between the counting of electoral votes in Congress and the inauguration, the Vice President elect will become President, according to Section 3 of the 20th Amendment."
Section 3 of the 20th Amendment is as follows:
"Section 3. If, at the time fixed for the beginning of the term of the President, the President elect shall have died, the Vice President elect shall become President. If a President shall not have been chosen before the time fixed for the beginning of his term, or if the President elect shall have failed to qualify, then the Vice President elect shall act as President until a President shall have qualified; and the Congress may by law provide for the case wherein neither a President elect nor a Vice President elect shall have qualified, declaring who shall then act as President, or the manner in which one who is to act shall be selected, and such person shall act accordingly until a President or Vice President shall have qualified."
As a general rule, if the president is is incapacitated (or dead as per your question), the Presidency defaults to the Vice President.
Horace Greeley actually did die before the electoral college votes were counted in 1872. Some of his electors voted for different candidates. Some voted for him. The ones that voted for him were thrown out by Congress.
Ultimately it didn't matter in 1872. Grant would have won in a landslide even if a single candidate had gotten all Greeley's electoral votes. But this does provide a precedent if it would happen in the future. His electors were allowed to choose otherwise valid candidates. Votes for the invalid Greeley were tossed.
Note that if no valid choice gets a majority of the electoral college vote, the House would get to choose. It's a little complicated in that they are supposed to choose from among the three top candidates in electoral votes. It's not clear what would happen if they replaced a dead winner with a fourth candidate.