Using different blockchain explorers one can retrieve the wallet balance or transactions of address. My question is how does this query works do they search the whole blockchain for the related address transactions and calculate the wallet balance? If so isn't it time consuming to do it this way ?
The process you describe is essentially correct. There is no way around that.
To improve efficiency, the results can be calculated in advance by processing the blockchain just once. Obviously this would result in an extremely large database of addresses and balances in order to provide faster lookup.