It's because the primary system favors candidates who pander to narrow slices of the voting public.
Primaries have low turnout: Most elections are between two unpopular candidates who are chosen from vocal political minorities.
According to https://en.wikipedia.org/wiki/2024_Republican_Party_presiden..., there were ~22 million voters in the Republican presidential primary, ~17 million voted for Trump. (~17 million voted in the democratic primary)
According to https://en.wikipedia.org/wiki/2024_United_States_presidentia..., there were ~139 million voters in the main election.
So roughly 12% of voters got Trump to be the candidate. What if the other 72% showed up to the primaries and got different candidates?