You apply from your country of residence mostly. If you are residing in Germany with a work visa you apply from there, you don't have to go back to your home to apply for a visa for any 3rd country.
This does not usually apply to people who are on a visit visa in the country they wish to apply from.
Every country has different residency requirements for visitors hence unless you specify some countries its hard to list all in an answer.
For example, US does not put any conditions on the applicant to apply only from the country of citizenship but then the onus is on the applicant to prove significant ties to the country of residence. So even in cases where its perfectly fine to apply like that its not going to be a piece of cake.
For another example, UK has this to say
1. Applications for short-term visas and EEA family permits
You can make applications for a visit visa, a short-term study visa or EEA family permit in any visa application centre, British diplomatic mission or consular post overseas where entry clearance applications are accepted.
You should be present legally in the country or territory you’re applying from.
2. Applications for other entry clearance
Applications for other types of entry clearance (except for Tier 5) should be made in your country of residence. ‘Country of residence’ might not be your country of origin and refers to wherever you have permission to live, for example for work or study purposes. Many countries issue overseas nationals with residency cards in these circumstances. If you have permission to stay in a country as a visitor for 6 months, for example, you are not a resident.
Where there is no post in the country where you live, you should make the application to the appropriate application post.