Edit: this is indeed a duplicate, I had not read the question carefully enough. Below is how you compute the distance from a point to a line, which is the major bulk when computing the distance from a point to a line segment.
Let us assume we are in $\mathbb{R}^n$ ($n\geq 2$) equipped with its usual Euclidean inner product $(x,y)=\sum_{k=1}^nx_ky_k$.
Let $L$ be a line parameterized by
$$
t\longmapsto P+t\vec{u}
$$
where $P$ is a point belonging to this line and $\vec{u}$ is a vector giving the direction of $L$. If you know two points $P,P'$ on the line, it suffices to take $P$ and $\vec{u}=\vec{PP'}$.
Now let $Q$ be any point. The distance $Q$ to $L$ is the distance between $Q$ and $Q_L$ its orthogonal projection on $L$. Now $Q_L$ is characterized by the vector projection formula:
$$
\vec{PQ_L}=\frac{(\vec{PQ_L},\vec{u})}{\|\vec{u}\|^2}\vec{u}.
$$
So
$$
\vec{QQ_L}=\vec{QP}+\vec{PQ_L}=\vec{QP}+\frac{(\vec{PQ_L},\vec{u})}{\|\vec{u}\|^2}\vec{u}.
$$
It only remains to compute the norm of the latter to get the distance from $Q$ to $L$.
Note: when $n=2$ and $L$ is given by a cartesian equation $ax+by+c=0$, this yields the formula
$$
d(P,L)=\frac{|ax+by+c|}{\sqrt{a^2+b^2}}
$$
for every $P=(x,y)$.
Algorithm to compute the distance from $Q$ to the line segment $[P,P']$: Take an arbitrary point $Q$. Compute the coordinates of the projection $Q_L$ on the line (which does not necessarily belong to the segment). Compute $d(Q,Q_L)=\|\vec{QQ_L}\|$ the distance between $Q$ and $Q_L$. Also compute the distances $d(Q,P)$ and $d(Q,P')$ to the endpoints. Then the number you are looking for (the distance from $Q$ to $[P,P']$) is the minimum of these three numbers: $d(Q,Q_L)$, $d(Q,P)$ and $d(Q,P')$.