In the US, it seems that in certain states, the political parties have allowed the state to prescribe the manner in which delegates are selected to the party's national convention. This includes things like setting the requirements for a candidate to appear on the ballot and party affiliation requirements for the voters. Since this all really seems like internal party matters, how did it come to be that the states have so much say over how a political party selects its delegates to their national convention? Did the parties willingly give this power to the states? (Could they take it back? ... this part is less history related, so feel free to ignore)
Your question seems focused on just federal elections, so that is where I will direct my attention. The reality is that the states, and the federal government, have always had an implicit power to dictate the terms of party elections, so the parties never really gave up this power. The mechanics of elections have always been out of the hands of the parties, but the substance of parties has always been within the province of the parties.
To better explain you need to look at the following: 1) the pertinent legal concepts, 2) the history of state election laws, and 3) whether the parties can do anything about it.
1) The Pertinent Legal Concepts
It is best to start at the beginning, ie, The Constitution. Art. 1, Section 4, states:
The important point here is that the federal government can pass regulations related to federal elected offices, and the state governments can control elections via the "Times, Places and Manner". However, the federal government has much more limited power with respect to the election of state officials. Since your question is limited to primary elections I won't go into how the federal government can get involved via your voting rights, etc. For further reading see 91 Corpus Juris Secundum United States § 18
It's not just the states that have the power though, the actual elected officials have power as well. Art. 1, Section 5, of the Constitution states in relevant part:
This is important because both the House and Senate can determine whether or not elected officials can sit for their seats. There are of course limitations on this power.
Another important Constitutional concept to keep in mind is that you have a "freedom of association" right which limits what the states and the federal government can do with respect to primary elections. See First Amendment to the Constitution.
In sum, the states can dictate the "Times, Places, and Manner" of elections, but they do not have unlimited power to regulate (see Tashjian v. Republican Party of Connecticut, 107 S.Ct. 544), yet as a practical matter they do have a compelling state interest in making sure elections aren't shams, even primary elections. See Burdick v. Takushi, 504 U.S. 428. The federal government can regulate in all sorts of ways, but where it comes to affect primary elections for federal office most are in laws that: govern the election of the president; dictate the date of elections for federal office; and the definitions of what is an election, candidate, etc. for federal office with respect to federal election laws. For more on federal election law check out USC Title 2.
2) The History
Your question would be better phrased as "how have the states historically asserted their powers over political parties in the election context?" The answer to this goes hand in hand with the development of the party system in the US.
It would be extraordinarily difficult to dive into the ways in which each state regulated elections at the beginning of the country, so instead I'll look at how presidential elections have been governed first.
The election of 1796 was the first time that presidential candidates were members of a political party. The system in place then had the states dictating electors who would then cast two votes for president, one for a home state person, and one for someone from out of state. As you can imagine, this system didn't last very long for various reasons. For further reading go here.
It was not until 1831 that the first political party would have a national convention when the Anti-Masonic Party had a national convention in Maryland. This started to formalize things nationwide.
It appears that the first primary election law was passed in California in 1866, and dealt with ensuring the integrity of the primary elections there. New York also passed a similar law in 1866. Subsequently in 1871, Ohio and Pennsylvania passed their own laws. These initial laws were concerned with the question of corruption, and tried to instill voter confidence in the party system.
It was the beginning of the 1890s that the states really began to get involved in the party system when they passed laws stating that the primary elections be paid for with public funds. These first sets of primary laws were often not generally applicable statewide, but that started to change between 1901-1904 as Minnesota, Mississippi, Wisconsin, and Orgeon passed laws regulating the conduct of all elections in their state. I highly recommend reading the following source already cited Primary elections; a study of the history and tendencies of primary election legislation writen by Charles E. Merriam in 1908.
The history tells us that the states really started to become heavily involved in the political primary process out of a concern with corruption. At that point in time the political parties were firmly entrenched across the country, so they had become de facto components of the election system, and the states had no choice but to regulate them.
3) Can the parties do anything about it?
Not really. They would have to show that these regulations with long standing precedents somehow violate their constitutional rights.
Well, technically the states don't have to do anything at all for the parties. So, if the parties would like their primaries to show up on state ballots counted at state expense by state-funded officials, then they have to follow the rules (laws) the state sets for such activity.
There's nothing stopping you from starting your own party and selecting your own candidates through whatever arbitrary means you like. However, if you want to show up on state ballots you have to follow state laws.
Note that mostly the state governments just restrict themselves to roughly when they will allow primary elections, and how a party goes about getting their candidates to appear on ballots. Parties themselves are mostly free to choose how they pick their nominees. For most elections its easier to just use whatever mechanisims the state has in place for counting ballots in elections.
However, for presidential candidates things often get quite a bit more complicated (actually, insanely complicated might be more accurate).
The two major parties select their candidates at conventions, and give each state a certain number of delegates to the convention. Simple enough, right?
Typically, every statewide elected official is automatically a delegate (often called "superdelegates"), and then the state party gets to chose how they will select the rest of their delegates. There are about as many methods are there are states. Many just do a simple election, with the statewide winner getting to pick all their own delegates. Some do the same, but divvy the delegates between the candidates roughly proportionally to the votes the candidates received. Some instead do a caucus system where everybody gets in a big room and hashes out verbally who the room will vote for. Texas has some weird hybrid system that almost defies explanation. To make it even weirder, the Republican Party this time around is taking half the delegates from states that tried to vote earlier than they are supposed to, and forcing other "early" states to use the proportial delegate selection system.
So, it's complicated.