In the United States, elections are almost always two stage. In the first stage, people run for the party's nomination. In order to do this, they have to get signatures from people who have registered as that party. Those signatures, possibly combined with a signup fee allow them to participate in the primary election. This applies to the two major parties, the Republicans and the Democrats. Other parties usually do not have government financed primaries, although there are some exceptions, e.g. New York and Alaska.
In the second stage, only the winners of the first stage are able to claim to be Democrats and Republicans. Third parties nominate their candidates separately and depending on their status either get automatic participation like the Democrats and Republicans or more commonly get signatures to get on the general election ballot. Status is normally derived from performance in other elections. For example, the United We Stand party had ballot access in some states in 1996 and 2000 because of Ross Perot's performance in 1992.
There are some states that don't use the primaries to pick the party winners but the top two winners. In those states, two Democrats or two Republicans could be the top two winners. This happened in the California Senate race in 2016. Both Kamala Harris and Loretta Sanchez qualified as Democrats for the first ballot (based on signatures), so both advanced as Democrats.
Excepting those states with non-partisan primaries, as a general rule, a candidate has to win the primary or be selected by some other party process to appear on the ballot as whatever party.
For example, can the candidate declare themself to be a Republican or Democrat even if the party has nominated someone else?
No. Either the state doesn't have party nominations (California, Washington, Louisiana) or the candidate will not be listed under that party unless nominated. Note that states without party nominations may not allow third party or write-in candidates in the general election.
Joe Lieberman actually lost the Democratic party nomination in 2006. He appeared on the general election ballot under his own party (Connecticut for Lieberman).
It might be more accurate to say that the party cannot officially nominate someone outside the government-sponsored primary. I mean, the party organization could do so. But that person would not be on the ballot, so it would be a futile exercise. And that person would generally not get listed as a Democrat even if on the ballot somehow, even if the "party" wants that.