A generic definition can be given in terms of algebraic structures.
Given a functor $T : \mathcal Set\to\mathcal Set$, a $T$-algebra is a pair $(A,\alpha)$ where
$$\alpha : T A \to A.$$
For groups, $T$ is defined by (note that a map $f : A+B\to C$ is a pair $f_1+f_2$ where $f_1 : A\to C$ and $f_2 : B\to C$)
$$T A := 1+ (A\times A)+ A.$$
A group is then a pair $(G,\alpha)$ (technically, we should also specify the group axioms this pair satisfies), with
$$\alpha : 1+ (G\times G)+ G\longrightarrow G$$
where we take
$$\text{id}_G := \alpha_1(*)\qquad g \star_G h := \alpha_2(g,h)\qquad g^{-1} := \alpha_3(g).$$
Now given two algebraic structures $(G,\alpha),(H,\beta)$, we define a homomorphism $k : (X,\alpha) \to (Y,\beta)$ as a map making the following diagram commute:
$$\require{AMScd}
\begin{CD}
T X @>{T(k)}>> T Y\\
@V\alpha VV @VV\beta V \\
X @>{k}>> Y
\end{CD}$$
where $T(k)$ lifts the map $k$ to $TX\to TY$ in a natural way (this is implicit by the functoriality of $T$).
For groups, say we have $(G,\alpha), (H,\beta)$ and a map $k : G\to H$, then the natural lifting of $k$, $$T(k) : 1 +(G\times G)+G\longrightarrow 1 + (H\times H) + H$$ is given by the triple $k_1 + k_2 + k_3$ where
$$k_1(*) := *\qquad k_2(g,h) := (k(g),k(h))\qquad k_2(g) := k(g)).$$
Now we can apply the condition above to $(*,(g,h), g)\in TG$ to obtain
$$k(\alpha(*,(g,h),g)) = \beta(T(k)(*,(g,h),g))$$
If we unpack this, this means
- $k(\text{id}_G) = k(\alpha_1(*)) = \beta_1(*) = \text{id}_H$
- $k(g \star_G h) = k(\alpha_2(g,h)) = \beta_2(k(g),k(h)) = k(g) \star_H k(h)$
- $k(g^{-1}) = k(\alpha_3(g)) = \beta_3(k(g)) = (k(g))^{-1}$
which is precisely the definition of a homomorphism for groups, although in the case of groups we can derive conditions 1 & 3 from 2.