Most of us learned through trial and error, but sometimes it's good to get tips from others on better ways to do it. Some use simple tools like a toothpick to wedge the loop of a string into its peg on the tailpiece (Gibson style model). Some use a capo to hold down the string until it's tensioned close to pitch.

We like using a peg winder and keeping a tuner close by. Once the loop end is seated, there's also a way of wedging the string into place with the fleshy forearm while you wrangle the tuning end.

We all have our own ways, and here is a video from the D'Addario Strings website using one of their tools: