Let's say I am going to visit country A before visiting country B. Can I apply for the visa of country B before applying for the visa of country A?
Yes, you can apply for the visas in reverse order, and if you are planning to visit them on the same trip, you should do so.
The reason for this is that Country A, in the middle of your itinerary, might not grant a visa unless they are satisfied that you can legally enter the country of your next destination, Country B. This is most commonly seen with Schengen states, though the UK and USA are also interested, as are other countries.
So therefore, you should already have the visa for Country B, at the end of your itinerary, in your passport before you apply for the visa for Country A.