On the automatic transmission is an NSS(neutral safety switch).
Manuals have a Clutch switch.
When you turn the key to START, 12volts runs to NSS and then to starter solenoid, closing it and cranking the engine.
Battery--------Key-------NSS------Solenoid
For the NSS to be closed(passing 12volts) the transmission must be in Park or Neutral, holding the NSS closed.
A few things can happen:
NSS could be going bad
NSS connector could be loose
NSS is adjustable so could be out of adjustment
Transmission shift cable could be loose, so while it is going into park it might not be pushing the NSS in all the way.
Rangers have a common issue with automatic's shifter loosening up in the steering column
NSS is usually just above the shift linkage on the transmission
It can also be a combo switch that turns on the reverse lights as well