I have a line given by two 3D points and a triangle given by three 3D points.
How could I find the point on the triangle with the smallest distance to the line?
On
I think a complete answer builds on that given by Joseph O'Rourke (Joseph O'Rourke (https://math.stackexchange.com/users/237/joseph-orourke), Find the closest point to a line on a triangle in 3D space, URL (version: 2017-11-28): https://math.stackexchange.com/q/2541076) which is correct in many instances. However, when the line segment is "in line" with the triangle, the closest point will not be on the edge of the triangle, but within it. There is a difficulty of choosing the point if: in this "in line" case, the line segment is parallel to the plane of the triangle; and, if the line segment is parallel to one of the edges and is not "in line". A line segment is "in line" with a triangle if it does not cross the 3 planes given by each edge of the triangle and the normal of the plane, and lies within those planes. This answer might help those interested in calculating the distance between the line segment and the triangle.
Intersect your line $L$ with the plane containing the triangle $T$. If $L$ pierces $T$, then the distance is zero. Assume henceforth that $L$ misses $T$.
Now I claim that the minimum distance between $L$ and $T$ is realized by a point on the boundary of $T$. So now your problem is reduced to finding the minimum distance from a segment to a line $L$ (repeated three times, once for each edge of $T$).
Segment-line distance is discussed for example at geomalgorithms.