There are several processes that cause congestion to seemingly appear out of nowhere only to slowly vanish as you drive through it. Flow on a freeway is constrained by a small number of critical locations, referred to as bottlenecks. When demand exceeds the capacity of a given bottleneck it becomes active, and it is not able to serve all drivers exactly when they arrive. These drivers thus have to wait in a queue until there is space for them to pass through the bottleneck, and the delay is manifest as reduced speeds in the lineup. To illustrate this situation, consider a bottleneck where three lanes drop to two. Those two lanes limit the flow of vehicles that can pass the site of the lane drop to the capacity of two lanes, even though demand can be as high as three lanes' worth of traffic. When active, the bottleneck will serve vehicles at capacity and the queue can grow to stretch for several miles if it persists for a long time. The rate at which the queue length grows or shrinks is determined by the difference between upstream demand and bottleneck capacity. An analogous situation would be water flowing into a bucket with a hole in the bottom. The hole is the limiting factor and represents the bottleneck, whereas the water level represents the queue length. If there is some water in the bucket it will flow out at a relatively constant rate, independent of how fast water is entering from above, but the rate water enters the bucket determines whether the water level will rise or fall.
The second process governing traffic jams has to do with how drivers interact. For example, consider the impact of on-ramps. If we were monitoring traffic flow in the three lanes upstream of the lane drop example mentioned above, each on-ramp might be expected to contribute 1/6th of the flow at that location (each lane carries 1/3 of the flow and the on-ramp contributes 1/2 of the flow in the outside lane). In other words, these entering vehicles consume capacity otherwise available to serve those already on the mainline at the given location. If you move upstream from the bottleneck against the flow of traffic, passing four such on-ramps (and no off-ramps), the effective mainline capacity drops to 0.48 of the bottleneck capacity. Because the average time per vehicle passage is the inverse of capacity, each drop in local capacity results in increased delays and, thus, speeds decrease.
Typical capacity of a freeway lane is about 2,000 vehicles per hour (vph). The two-lane section freeway segment can accommodate a total of 4,000 vph, whereas the three-lane section can handle up to 6,000 vph. Conservation of vehicles dictates that input must equal output, so the flow of queued vehicles in the three-lane section is limited by downstream capacity of 4,000 vph, and any demand in excess of this will cause the queue to grow further upstream as the vehicles wait to pass the bottleneck. Now moving upstream to the first on-ramp, within this queue it would be expected to provide up to 670 vph, leaving room for 3,330 vph that can pass the ramp on the mainline. Because this loss of capacity is multiplicative, by the fourth such ramp the local mainline capacity has dropped to 1,930 vph across the three lanes, which is less than the capacity of a single freeway lane.
Drivers move in the downstream direction with the flow of traffic, however, and indeed seemingly first encounter the end of the queue out of nowhere because the location depends on the queue length (how high the "water level" has risen), which is a function of earlier demand. The end of the queue typically has the worst conditions, which slowly improve as drivers progress through the queue. The steady improvement makes it easy to miss the actual bottleneck at the "front" of the traffic jam when you finally pass it. The task of identifying the bottleneck is compounded by the fact that drivers changing lanes before reaching a bottleneck can move some of the impacts upstream of the original source and the fact that many bottlenecks are not as obvious as the lane drop described above. In both cases the impacts may be very subtle and hard to perceive as you drive through them. For example, if most drivers tend to slow ever so slightly or allow larger spacing at a specific location, both of these factors increase the time per vehicle passage and thus reduce the capacity at that location. Even a small drop in capacity can cause the location to become the most limiting one on the facility, much like a kink in a garden hose. Such a capacity drop is almost imperceptible at the bottleneck, with the impacts becoming noticeable upstream only after being amplified through driver interactions. Of course, there are also many modifying conditions within the queue, such as vehicles exiting at off-ramps (thereby relieving some of the congestion), but this brief example illustrates the basic phenomena at work.