The origin of this phrase is nautical and refers to the ends of the countless number of ropes on early sailing ships.
These “ends” needed to be bound tightly to prevent unravelling, which could cause disaster at sea.
Whenever a captain noticed that his men had too much time on their hands, which could lead to trouble, he would order them to check the ropes and repair any “loose ends.”