Your local voter registration office, when you register to vote, will allow you to "declare" for one party or the other.
The idea is to prevent people who would vote for one party no matter what to go and vote in the other party's primary. Some states have written this into law. Some states you can register one way and vote in all the primaries if you choose.
Remember that each political party can, and does, make decisions however it likes. The Republican party recently changed its rules midstream during the convention just to keep attention away from Ron Paul.
The other purpose of declaring for a party, if you choose to do so, is that then they can pester the living shit out of you to raise money. My wife registered under one of the parties. They call dozens of times each year looking for cash.
You have to be registered with the government as a voter in order to vote at all. When you register, you list your party affiliation. If it is one of the major parties, that information also adds you to the party's list of members, granting you the ability to vote in their primary elections. If you register as an independent (e.g. unaffiliated) voter, it depends on what state you're in, but sometimes you can vote in either primary and sometimes none at all. Note that there is also an Independent Party, which is different from lowercase independent, meaning no party affiliation at all.
The vote you place is anonymous. Your vote is not tied to your voter registration, but you have to prove that you have registered in order to place that vote.