Usually you would pay the dividend tax in your country of residence (tax residence) depending on which country you are based in and double tax agreements. If country A also has you on the hook with their tax laws the double tax agreements would -usually- shift that over to country b. Also make sure that country A company is actually considered a country A company or in reality country B company by country B since you are UBO etc pp otherwise you will be on the hook for corp taxes in country B at their corp tax rate.
@jackfrost Thanks for the info. I would actually not spend time in country A but rather spend 80% of my time in B. The company is formed in A and will have an physical office there as well. Local laws in A require 5% dividend tax so I thought I would not need to pay any tax on these amounts in country B.
Well from the sounds of it you are tax resident in country B then (does not need to be even with 80%)
Just an office is usually not enough to not make country B declare your company as a country B resident / taxable company if you hold the majority of shares. You will need to check your country B CFC / foreign income / management whatever laws.
Lets suppose you pass country Bs substance tests for those and it it is ok with company being a country A company and being taxed in country A then you should probably check the double tax agreement between your two countries. They will have a dividend / passive income clause that will tell you which country is allowed to tax you in which way on those dividends. You might get taxed in both countries but country B might give you a tax credit for the 5% paid in country A. It is very unlikely (unless we are talking Malta, Cyprus etc) that you are not going to pay proper dividend taxes in country B with country B's dividend tax rate.
The double tax agreement will also have clause about what constitutes proper local offices / substance tests in which country and what makes which country be the tax receiver for said company or local office / subsidiary of said company.
Mind you this is all very dangerous private knowledge from having to deal with it myself so just a starter for you to know where to look. I don't understand a lot of those parts correctly myself. Final word should come from a tax lawyer - i would always advise two independent lawyers.