What is the essence of Kernel or Null Space? I describe Kernel in this way: Kernel(In group theory, Linear Algebra, etc.) measures the degree to which a map or morphism fails to be injective. In "finite dimensional" case, it also measures the degree to which a map or morphism fails to be surjective.
How to give a more general definition of Kernel, instead of just defining them on a special Algebraic structure, such as groups?
 A: In a category $\mathsf{C}$ with a zero object $0\in\text{Ob}(\mathsf{C})$, that is an object which is both initial and terminal, we may define the zero morphism $0\in\text{Mor}(\mathsf{C})$.
Indeed, the composition $X\longrightarrow 0\longrightarrow Y$ defines the zero morphism. If our category is closed under equalizers, we can define a kernel of a morphism $f:X\to Y$ as the following equalizer:
$$K\stackrel{k}{\to}X\overset{f}{\underset{0}\rightrightarrows}Y$$
You may wish to read the definition of a monomorphism and an epimorphism in a general category.
You mention measuring how far a linear map is away from being injective and surjective. Say that the linear map is injective, then the kernel is trivial. If the linear map $h:A\to B$ is surjective, then the cokernel is trivial:
$$\text{Coker}(h)=B/\text{im}(h).$$
One may wish to define the cokernel category theoretically:
A cokernel for $f:A\to B$, $\text{Cok}(f)$, is a pair $(C,p)$ where $p:B\to C$ is an epimorphism such that $pf=0$, and such that any other morphism $h:B\to Y$ with $hf=0$ factors through $p$. This is an example of a universal construction.
One may with to then show that the image is simply the kernel for a cokernel, and that a coimage is the cokernel for a kernel.
A: What’s not mentioned yet: given  a homomorphism $f\colon G\to H$ between two groups (or vectorspaces) then one may establish a new group $G/\ker(f)$ via equivalence relation.  Prominent example: $f\colon\mathbb Z\to\mathbb Z$, $x\mapsto2x$. Then $\ker(f)=2\mathbb Z$ so $f$ produces the famous $\mathbb Z/2\mathbb Z=\mathbb Z_2$.
A: In category theory (which is the most general field of mathematics), the kernel of a morphism $X \overset f \longrightarrow Y$ in a pointed category is the equalizer of $f$ with the zero morphism $X \overset 0 \longrightarrow Y$.

Explanations


*

*Category theory: it seeks to generalize quite a lot of mathematics, but especially those related to algebra. A category is a set/class of objects together with a set/class of morphisms between every pair of objects (which can be empty for some pairs) denoted $M(x,y)$, that satisfies some properties:


*

*For every $f \in M(x,y)$ and $g \in M(y,z)$ there exists a morphism in $M(x,z)$ denoted $g \circ f$.

*For every $f \in M(x,y)$, $g \in M(y,z)$, and $h \in M(z,w)$, we have $(h \circ g) \circ f = h \circ (g \circ f)$.

*For every object $x$, there is a morphism called $\operatorname{id}_x$ (the identity morphism) such that for every $f \in M(x,y)$, $f \circ id_x = f$, and for every $f \in M(w,x), \operatorname{id}_x \circ f = f$.



An example is the category of $\bf Set$, where the objects are sets and the morphisms are functions between sets. Another example is the category of $\bf Grp$, where the objects are groups and the morphisms are homomorphisms between groups.


*

*Initial object: An initial object is an object $I$ such that for every object $X$, there is a unique morphism $I \longrightarrow X$. Not every category has an initial object.

*Terminal object: An terminal object is an object $T$ such that for every object $X$, there is a unique morphism $X \longrightarrow T$.

*Pointed category: A pointed category is a category where zero objects exist. A zero object is an object that is both an initial object and a terminal object.

*Zero morphism: For every object $X$ and $Y$, there is a unique morphism in $M(X,0)$ and a unique morphism in $M(0,Y)$, where $0$ denotes the zero object. Then, the composition is called the zero morphism $X \overset 0 \longrightarrow Y$.

*Equalizer: For a pair of morphisms $X \overset f {\underset g \rightrightarrows} Y$, the equalizer of $f$ and $g$, if it exists, is an object $E$ together with a morphism $E \overset {eq} \longrightarrow X$ such that $f \circ eq = g \circ eq$, and such that for every other object $E'$ followed by a morphism $E' \overset {eq'} \longrightarrow X$ such that $f \circ eq' = g \circ eq'$, there is a unique morphism $E' \overset h \longrightarrow E$ such that $eq \circ h = eq'$.

The definition of equalizer is rather convoluted.
It states that, given the following diagram:
$$X \overset f {\underset g \rightrightarrows} Y$$
An equalizer of $f$ and $g$ is an object $E$ together with a morphism $E \overset {eq} \longrightarrow X$ such that this diagram commutes:
$$E \overset {eq} \longrightarrow X \overset f {\underset g \rightrightarrows} Y$$
(A commutative diagram is one where "all directed paths in the diagram with the same start and endpoints lead to the same result", to quote Wikipedia.)
Additionally, the object and the morphism has to satisfy the condition that for every other object $E'$ and morhpism $E' \overset {eq'} \longrightarrow X$ such that the following diagram commutes:
$$E' \overset {eq'} \longrightarrow X \overset f {\underset g \rightrightarrows} Y$$
Then there exists a unique morphism $E' \overset h \longrightarrow E$ such that the following diagram commutes:
$$\begin{array}{c}
&& E' \\
& \swarrow \small h & \downarrow \small {eq'} \\
E & \overset {eq} \longrightarrow & X & \overset f {\underset g \rightrightarrows} & Y
\end{array}$$
The name is due to fact that the morphisms $f$ and $g$ become "equal" if restricted under the image of $eq$.
Also, in $\bf Set$, the equalizer can be constructed as $\{x \in X \mid f(x) = g(x)\}$, which is a subset of $X$, so the morphism $eq$ is the inclusion morphism, i.e. $eq: x \mapsto x$.

Note that $\bf Set$ does not have zero objects (its initial object is the empty set and its terminal objects are singletons).
However, $\bf Set_*$, the category of pointed sets, does have zero objects. In this category, objects are tuples $(S, s)$ where $s \in S$, and a morphism $(S, s) \overset f \longrightarrow (T, t)$ is a function $f:S \to T$ such that $f(s)=t$. The zero objects are objects in the form $(\{x\}, x)$.

Usually, an injective function is a left-invertible function, i.e. a function $f:X \to Y$ such that there exists a function $g:Y \to X$ with the property that $g \circ f = \operatorname{id}_X$.
Such a morphism in category theory is called a split monomorphism.
However, we usually consider monomorphisms instead, which are left-cancellative functions. All split monomorphisms are monomorphisms, but not all monomorphisms are split monomorphisms.
If a morphism $X \overset f \longrightarrow Y$ in a pointed category is a monomorphism, then the zero object is the kernel of $f$, where the proof is omitted as an exercise.
However, the converse is not true, as can be seen in $\bf Set_*$.
