A general existence result for the Toda system on compact surfaces