More generally, a homomorphism is a function between structured sets that preserves whatever structure there is around. Even more generally, ‘homomorphism’ is just a synonym for ‘morphism’ in any category, the structured sets being generalised to arbitrary objects.
of the underlying sets that respects the binary operation in that for all in we have
This definition gives us the correct notion of magma homomorphism, semigroup homomorphsim, and group homomorphism, but it is actually a bit of a coincidence that it works for groups. It does not give the correct definition of monoid homomorphism, since it doesn't properly treat the identity elements. (However, the correct notion of monoid isomorphism can still be constructed from this inadequate definition of homomorphism.)
A homomorphism between two monoids and is a semigroup homomorphism
It is a theorem that a semigroup homomorphism between groups must be a monoid homomorphism (and additionally must preserve inverse elements, which is also necessary to be the correct definition of group homomorphism.)
A ring homomorphism is a function between rings that is a homomorphism for both the additive group and the multiplicative monoid. Traditional ring theory sometimes actually uses rng homomorphisms even when the rngs in question are assumed to have identity elements, so be careful when reading old books.
Generalizing further, we may simply treat ‘homomorphism’ as a synonym for ‘morphism’ in any category, although there is a strong tendency to use ‘homomorphism’ in the case of ‘algebraic’ categories: for example, nobody seems to speak of a homomorphism between topological spaces (continuous maps), or between manifolds. Here we put ‘algebraic’ in scare quotes to indicate the field of algebra; even morphisms in algebraic categories from other fields (such as the category of compacta) are not usually called homomorphisms.