There are considerations beyond winning the nomination or the election.
During the nomination process, standing for the nomination is a means of accomplishing a number of goals. The goals that are usually considered to be laudable include things that can be summed up as influence in the party, and increased visibility both within the party and in the wider community. The goals that are usually considered as suspect can be summed up as connecting to the party machine.
During the election process, even the loser can achieve a variety of goals. For example, they are frequently involved in debates and speeches that are picked up and carried by national and international news organizations. These are opportunities to inject ideas into the political consciousness. These ideas do not necessarily get implemented in the current election cycle. But they often remain as topics of discussion that get implemented at some later date.
The 2020 Democratic primary process was a reasonably good example of this. There were an unusually large number of candidates at the start of the process. We were treated to enough people on the stage during televised debates that the reporters resorted to asking for "a show of hands" on some questions. Some of those candidates have disappeared from public life. Others used it to achieve a certain amount of celebrity and so further their political life. Pete Buttigieg is an example, currently in the position of Secretary of Transportation.