This is unfortunately a very complicated issue. My wife made that experience more than once (she bears a Chinese passport) and it's one of her main motivations to get the Swiss passport as soon as she cans (beside political participation).
Since every country has its own rules there is simply no general rule. After all, you have to ask the representation of your destination country in the place you want to apply to. Only they can tell you for sure whether they will accept your submission of the application or not (note: This does not imply any acceptance of the application itself!).
Let's assume the following: You are a citizen of Country Z with a residence permit in Country A, planning to travel to country B.
You have good chances that your submission of an application is accepted if you have a long-term residence permit in Country A (let's say at least 6 months) and the entire validity of the visa you apply for lies completely within the validity of your residence permit. Please note: This does not imply any acceptance towards the application itself! Only the submission of an application may be accepted in such cases.
Some countries allow you to apply for tourist visa in places where you are staying on another visa if you have good reasons to do so (this may include: medical emergencies, family emergencies like accidents/sickness/death of close relatives, etc.), in rare cases a good reason consists already of a well-planned journey. I know of a guy who made it to obtain a Chinese visa in Korea after explaining his travel plans to the authorities there.
In general there is only one rule that applies to any case: The competent authority is the only contact point for reliable information - unfortunately even they can be wrong sometimes. I had the case of a visa of mine for China which was actually not possible to extend anymore but some local authority did - caused me quite some trouble when leaving the country...