In a restrictive sense, a homomorphism is a function between (the underlying sets) of two algebras that preserves the algebraic structure.
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.
Note: The word “homomorphism” has also traditionally been used for what we call a (weak) 2-functor between bicategories.
Traditionally, a homomorphism between two magmas $A$ and $B$ is a function
of the underlying sets that respects the binary operation in that for all $a_1, a_2$ in $A$ 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 rng homomorphism is a function between rngs that is a homomorphism for both the additive group and the multiplicative semigroup. (For rings with identity, this is again inadequate.)
A homomorphism between two monoids $A$ and $B$ is a semigroup homomorphism
of the underlying semigroups that preserves identity elements in that we have
This definition of monoid homomorphism is a special case (via delooping) of the definition of functor.
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.
More generally, a homomorphism between sets equipped with any algebraic structure is a map preserving this structure. This can be made precise using Lawvere theories, monads, etc.
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.
homomorphisms are to functions as logical relations are to relations