Indeed, the first compound in your sketch is numbered incorrectly according to IUPAC. IUPAC rules for branched hydrocarbons say: "When series of locants containing the same number of terms are compared term by term, that series is "lowest" which contains the lowest number on the occasion of the first difference. This principle is applied irrespective of the nature of the substituents. " (emphasis mine). In your top case, this means that you have two numbering choices: either 2,2,4 or 2,4,4. The 2,2,4 choice has a lower locant at the first point of difference, so it is preferred. When you construct the name, you indicate the substituents in alphabetical order, so the full name of the top compound is 4-chloro-2,2-dimethylpentane.
If unsaturations are present, then these have a higher priority than alkyl and halogen substituents. From the rules for unsaturated hydrocarbons: "The chain is so numbered as to give the lowest possible numbers to the double bonds." This rule is sufficient to impose the numbering you report.
As Loong correctly pointed out, the links provided above refer to the text of the now obsolete 1979 recommendations. Both the 1979 and 1993 recommendations are available on the same site, maintained by ACD Labs with IUPAC's permission, and are in accordance with what shown above.
Unfortunately, the text of the most recent (2013) recommendations is not freely accessible (that I know of). This is a link to the electronic version of the book, published by The Royal Society of Chemistry. The relevant section is P-14.3.5 in Chapter P-1 (General Principles, Rules, and Conventions): "The lowest set of locants is defined as the set that, when compared term by term with other locant sets, each cited in order of increasing value, has the lowest term at the first point of difference; for example, the locant set '2,3,5,8' is lower than '3,4,6,8' and '2,4,5,7'."