You transfer the hero to a Castle, and then you can buy a new one. You can keep heroes in Castles, for example, if the hero is resource- and money-oriented and equipped with appropriate artifacts (parts of the Legion + resource- and money-related artifacts), it will be very beneficial.
It is not recommended to keep too many heroes in Castles, as there is a very high chance that the Castle will turn gray, with all the consequences that follow.