To the OP, very roughly speaking:
The parties aren’t a government organization; they’re private clubs. For the purposes of this - it may help to think of them as a collection of private clubs that have a national club, a group of state clubs, and various local clubs.
The national club tells each state club (and/or district and/or territory), “We are voting for our candidate on this day. Show up on this date with ____ number of people ready to vote.”
Each state’s number of people is determined by the club’s formula - it gives some weight to the state population and it gives some weight to the number of people in that state who tend to vote for the party. It also gives weight to lots of other things. Each party uses its own formula - so the number of total delegates is different party to party, the number of delegates in each state is different party to party.
Each state/district/territory club needs to figure out both how they want to allocate their votes and who the people who will actually show up and cast them are. They are given some rules by the national club, but every state can do its own thing - within the rules that the national club has set up for everyone and the state club has set up for itself.
One common way of allocation is by primary, where everyone votes - and that vote may allocate people proportionally (you get 20% of the votes, you get 20% of the delegates), but not necessarily. It may be winner take all, it may be winner take all after a certain percentage, it may be winner take most, it may drop down to a different level (legislative, county, or congressional). There’s a lot of variance there - and again, within each state, each party may be doing something different. (There’s also a lot of variance in who gets to cast a vote - this again varies by state and by party). The other common way is by caucus, where people in a precinct get together and decide what they want to do next and who is going to represent them.
Then you do have to figure out who is going to cast those votes - the delegates (the state chair & her friends? elected on the primary ballot? large donors? large volunteers? chosen by the group of people at the caucus?) again, this changes from state to state/party to party. Also, what those delegates are supposed to do varies - it is not necessarily “the will of the people.” Some are tied to a particular candidate. Some were tied to a candidate who dropped out, and now are tied to a different candidate. Some were tied to a candidate who dropped out and are allowed to do whatever. Some have said that they were to vote a particular way. Some have no restrictions at all - but people suspect they’re going to do something. And of course, because they are people and this is America, once they show up on that day - they can really do whatever the heck they want. The penalties for voting “wrong” vary from party to party on the national, state, and perhaps even lower level. Some people find the consequences worth it.
Superdelegates: The Democrats have decided that as well as using their formula to determine how many votes each state gets, certain people get a vote. Governors, Senators, Representatives, former Presidents, state party chairs, etc. Each of them gets a vote - if they want it - and they can do whatever they want with it. (According to some people, the superdelegates influence the electorate. According to some, the electorate influences the superdelegates. There’s evidence both ways.). At this time, Republicans do not have superdelegates.