The SOFA (Status of Forces Agreement) would allow for entry exit and residency with official orders and an ID card.
If there is no SOFA in place then the host nation will decide how they handle military members in the country -The exception being Military Attaches and their staff. Those people are covered under Diplomatic Agreements.
They enter and exit with Official (Brown Cover) passports
Trainers and instructors brought in to an allied nation are managed on a case by case basis, but would also have brown official passports and a visa from the host nation.
Military members who enter a nation as part of hostile actions don't have any visa requirements
ETA, Most formal treaty signatory nations such as Korea, all of ANZUS and NATO have reciprocal agreements that allow service members to travel with the DD form 2 ID card and leave orders. So, if I was stationed in Japan I could take leave to visit Korea or New Zealand but would need a passport and comply with any Visa requirements to travel to non-allied nations such as PRC or Indonesia