Every state and political party has their own rules for registration and primary voting. As user1873 pointed out there are some states that allow you to vote without being registered in primaries, but I believe this is the minority of cases.
There are no laws restricting registration and how it is handled, so the political parties are free to set their own rules for registration. if they choose to say you can't register with them if you have registered with another party well in absence of any law preventing that they are free to do it. Likewise they are free to have closed primaries where you cannot vote if you are not registered because no law has been written forbidding it.
So the question really is why, in the majority of cases, do political parties prefer closed primaries as opposed to the open primaries of Arozona and a few other states? I'm not an insider with the political parties and so can't say for certain; however, I suspect it's a combination of two key concerns that inspires closed primaries.
Encouraging loyalty to the party.
By forcing a voter to pick your political party to be allowed to vote in your primaries you can ensure they focus their attention more on your party then on the other party. So, for example, if they vote for Candidate A in your primary after thoroughly considering all the various candidates your party has they are likely well informed about Candidate A's policies and beliefs. By contrast they may know comparatively less about the other political parties Candidate B, because there was less reason to research that candidate since they couldn't vote in the other parties primaries. That means you are starting your race for president with a voter that is more informed about your candidate then your opponent, which may ultimately sway the voter towards voting for the one they are more familiar with.
There are many similar arguments for party loyalty. The more that a voter sticks to one party they more they tend to fall into group think, the more frequently the vote for that party etc. Making someone feel 'part' of a party, by getting them officially registered for it and letting them in on the super secret primary voting, helps to make them feel more a part, and loyal, to your party, which in turn boosts the odds they will continue to vote for it. You don't get this sort of loyalty from an independent voting in both parties primaries.
Prevent sabotage votes.
If anyone can vote in your primary then in theory people who identify with the other party could vote in your primaries in an attempt to sabotage it by voting for the worst possible candidate. In reality I believe it's unlikely this would become a problem for a party. A few sabotage votes aren't going to sway an election by themselves so sabotage would only work if a political party went out of their way to organize a large scale sabotage campaign to encourage all their voters to make sabotage votes, and such an action is unlikely to end up helping the party that organized it once the backlash and negative publicity sinks in. Having said that there are some who are concerned about this threat, rather or not it's realistic, and to them closed primaries which theoretically prevent it are considered preferable to having a potential vulnerability.
I have to play devils advocate, I'm pro devil you know...
Now for the sake of playing devil's advocate I should point out the biggest downside to closed primaries, and the reason some states don't do that. This issue basically comes down to an inability to appeal to independent voters, which is imperative if you want to win presidency. The best way for a politician to win closed primaries is to be the most extreme support of their parties political views. However, someone who is extremely left or extremely right may not appeal to independent voters who are more likely to prefer a more moderate candidate. By allowing independents to vote in your primaries you increase the odds that a candidate will be picked that appeals to independents by allowing the independents to have a say in who your candidate will be.
Since Democrat's already have a partial protection against the tendency for primaries to lean towards the more extreme, and potentially less appealing to moderates, candidates in the form of super delegates arguable they would arguably suffer a little less from closed primaries then Republicans who don't have such protections, though I have not personally seen any study or analysis on this topic.