The why of stretching is very much dependent on your personal physiology and the sport you are doing. With a few exceptions, you don't really need to stretch much past normal range of motion. There is no evidence that it helps with injury prevention***, and evidence that stretching at the wrong time can impact performance.
There are 4 basic types of stretching, and times that each should be performed:
This is basically active movement that mimics the activity you are about to do, in increasing amplitude. For example, runners will do leg swings, walking lunges, high knee drills, butt kicks. Basically gets the muscles warmed up and loose so that you don't go into an activity cold, so this type of stretching should be done before, and possibly mid workout depending on your goals.
Pretty much what everyone thinks about when they think stretching. Sit and reach type activity. This is done to increase flexibility, but unless you are in a sport where specific flexibility is needed (Martial arts, gymnastics need overall flexibility. Hurdlers need leg flexibility esp in hamstrings, and so on.), it's not necessary to do. Static stretching done before a workout has been proven in studies to reduce power, and there has been no evidence that it helps with either recovery or injury prevention. This should be done either after a workout, or after a long warmup period, as static stretching on "cold" muscles increases injury risk.
The reach and bounce method. Popular in older times, then phased out, now making a bit of a comeback in specific training situations. The problem is that you can easily activate the stretch reflex in the tendons, which can easily result in tears/ruptures. It will, however, help train your body to recognize that movement at the extreme edge of the range of motion are ok, and there is some evidence that flexibility gains can be made faster than with static. Best done under supervision from an experienced trainer and again, after workout/warmup.
PNF (Proprioceptive Neuromuscular Facilitation)
Mostly partner stretches, but can be done solo for certain exercises. Basically your partner stretches your muscle (such as pushing your leg towards your upper body for front splits), and then resists while you contract the muscle being stretched for 5 or so seconds. As you relax, then then push the stretch deeper and hold it there for 20-30 seconds. Originally developed for rehab, it is effective, but should only be done once a week or so, as it is very intensive. Another one for after a workout/extensive warmup.
***Caveat on injury prevention
While there is no evidence that being flexible aids in general injury prevention, there are sports situations where not being flexible will introduce a greater chance of injury. Consider a first baseman reaching for an errant throw while keeping the back foot on the bag. As they step out and stretch, an inflexible person has a greater chance of lunging past their personal ROM and tearing something. I've had bike accidents where I stepped out for balance and my leg slid way wide. My ROM saved me from injury. So not quite injury prevention, but enabling a person to avoid injury in some specific situations.