How do central banks earn profit? I thought of Central banks as the regulators of financial markets. But surplus reserves of some Central banks surpass giant corporate companies.

Please explain me how do they manage to make profit in just regulating activity and printing currency.
Central Banks are not made to make profit. They are there to stabilize the price (inflation) and avoid crisis.

But to do that, they sell and buy assets. If they do a good job, they must purchase assets when they are undervalued and sell when they are overvalued. By doing that they are making profit.

This is good because Central Bank's infrastructure does have a cost. If they were not making profit, they would have to be financed by the taxpayer. When a central bank is making profit, they typically give this money back to the state, lowering the amount of needed taxes.