I've built up a stretching routine over the last few years. My methodology has been trial and error. But the general process works like this:
First, when I notice that something hurts or is tight, I try to identify the muscles near it. Then I search for dynamic and static stretches that target those muscles and I try to get about three or four.
I add those to my pre workout warmup and/or post workout stretches. After about a week of using them I might realize some aren't as useful as others or sometimes they're just redundant. So by a process of elimination I usually only keep one.
So over the years I now have a warm up and stretching routine for my hips, shins, knees, neck, back, and shoulders.