Generally, the police can demand identification under two circumstances: if you are suspected of a traffic offense, or if you are in a state with loitering laws and you are violating them.
If you are driving and are stopped by the police for a traffic violation, they have the right to demand identification.