What can I do to a toenail that is almost all the way off?

Remove the rest. If it is loose, the nail plate should be avulsed or clipped to allow a new nail plate to grow in. If it is hanging loose, the nail is vulnerable to being ripped off causing further injury or infection.
Best to remove. Trim away the loose parts of the nail. If there is still pain or drainage, see a doctor to help remove the painful edges. Hope this helps.