5. Ports $A$ and $B$ are on the south shore of a large lake, and port $C$ is on the north shore. Ports $A$, $B$, and $C$ form the vertices of an equilateral triangle. Ships from port $A$ and port $B$ start at the same time and travel in a straight line towards port $C$ at a constant speed. When ship B has traveled 40 kilometers, the positions of ships A and B and port $C$ form the vertices of a right triangle. When ship A reaches port $C$, ship B is still 20 kilometers away from port $C$. What is the distance between ports $A$ and $B$ in kilometers?
5. As shown in the figure, let $A$, $B$, and $C$ be the three vertices of an equilateral triangle with side length $x$ kilometers. Ships from port $A$ and port $B$ start at the same time. When ship B travels 40 kilometers to point $E$, ship A reaches point $D$. When ship A reaches port $C$, ship B reaches point $F$, which is 20 kilometers away from port $C$. It is given that ship A is faster, $\angle C D E=90^{\circ}$, $C D=\frac{1}{2} C E=\frac{x-40}{2}$, $A D)=x-\frac{x-40}{2}=\frac{x+40}{2}$, and $B F=x-20$.
From the problem, we have $\frac{A D}{A C}=\frac{B E}{B F}$, which is $\frac{\frac{x+40}{2}}{x}=\frac{40}{x-20}$.
Solving this, we get $x_{1}=30+10 \sqrt{17}, x_{2}=30-10 \sqrt{17}$ (discard).
Therefore, the distance between ports $A$ and $B$ is $30+10 \sqrt{17}$ kilometers.