In the US, people outside of the individual States cannot vote in the federal elections for President, Senators and Representatives.
Frame challenge: This is not true. Every state to some extent allows people registered to vote in that state to vote by mail. The extent to which this is allowed varies from state to state, but in general, people domiciled in a state who will for whatever reason be abroad, in some other state, or even in some other part of the state on election day are typically allowed to request to vote by mail (and to vote by mail), in many cases even for local elections. Even astronauts who are in space on election day can vote by encrypted electronic mail -- if they're domiciled in Texas. An Earthbound clerk sworn to confidentiality and to honesty transcribes the contents of the encrypted email to a paper ballot. (This is fairly new. The first time an astronaut was allowed to vote from space was in 1997.)
In some cases, even some people who have never stepped foot in the US can vote by mail. This capability varies from state to state; see Registering for a midterm election as a US citizen abroad (never-resident).
What is the process by which US military personnel are assigned a state in which their vote will be counted? Can they choose any state they like (i.e., is it a result of federal regulation), or do each state set requirements for a citizen to be counted there?
By default, it's the service member's "home of record" -- the state they came from when they signed up with the military. That said, service members can change the state in which they vote. They simply register to vote in the state in which they want to vote -- so long as doing so is legal in the state of choice and so long as they don't vote in their former voting residence.
Different states have different rules regarding voting residency. One very common rule is that registering to vote has to be done in person. Many states also have a residency requirement for voting, but this typically is not as long as the period of time needed to establish that state as the service member's legal residence.
For example, registering to vote in Texas and then voting in Texas is one of the pieces of evidence that a service member can use as evidence for changing their legal residence to Texas. (Obtaining a Texas driver's license, registering a vehicle in Texas, buying a home in Texas and declaring that as their homestead, having a bank account in Texas, and establishing a will in Texas also count.) Voting in Texas could not be used for legal residency if one first has to become a legal resident in order to vote there.
One's legal residence has tax implications. Changing one's legal residence from Maryland to Texas, for example, eliminates the state income tax paid to Maryland, and since Texas doesn't have a state income tax, service members who changes their legal residency to Texas pay less income tax.
When stationed abroad, service members typically cannot change their voting residency due to in-person requirements for registration, nor can they change their legal residency because this needs to be done while one is in residence in that state.
To implement a similar process to enfranchise US citizens from Territories (excluding American Samoa, which houses US nationals and not citizens) by assigning them a state, following the same process as in the military, would it be just a matter of state law, or is there federal legislation regulating this?
US citizens can move to any state and register to vote in that state. They simply have to follow the voter registration rules for the state to which they have moved. They of course have to have moved to that state. A number of former residents of Puerto Rico have done just that. (I wrote "former residents" because they eventually become residents of the state in which they registered to vote. They cannot, or at least should not (it's illegal), vote in Puerto Rican elections if they register to vote in a state.) America Samoa is an exception as people born there to non-citizens are themselves "non-citizen nationals." People born in other US territories are US citizens upon birth. People born to US citizens are US citizens regardless of where they're born. (They might not be eligible to run for US president or vice president.)