Just the same as when an adult has sore muscles, stretching can be one of the keys to relieving growing pains, and it can also work to prevent them in the first place. If your child is old enough, you can get them into a routine of stretching at the right times, but if the child is young, you may have to help them.
To prevent growing pains, you want your child to stretch at different times throughout the day to keep the muscles loose and ready for activity. If the child is getting ready to go out and play, then you should have them stretch before they start. Explain the importance of stretching and how it can be used to prevent the pain that is bothering them later in the day.
For kids that are old enough, teach them to perform stretching exercises like toe touches, leg raises, and lunges. If the child is young and unable to stretch on their own, you can help the child by assisting with the motions of the stretch. As was mentioned above, a great time for stretches is before physical activity. You may also want to have your child perform some stretches before bed.