If you're minding your own business and walking down the street, can a cop stop you and ask for your ID? Possibly.
Many states have "stop-and-identify" laws. Under these laws, if a police officer reasonably suspects that someone has engaged in criminal activity, the officer can detain that person and ask for identification. A person who refuses to provide identification commits the crime of resisting an officer's lawful order. (Hiibel v. Nevada, 542 U.S. 177 (2004).) Without that reasonable suspicion, however, a demand for identification may be illegal.
Drivers who are asked to produce an ID may need to comply as well. Laws in many states require drivers who are stopped for speeding and similar infractions to provide identification when an officer requests it.
In general, police can also stop and ask you questions on the street. But you typically don't have to answer any questions beyond identifying yourself and your presence. That said, you can run afoul of the law if you intentionally lie to officers. The best course of action is generally to provide your name and reason for being in that location, and then politely decline to answer any more questions and ask the officer if you may leave. For more tips, check out Talking to Police When You're Not In Custody.
Laws in many states define loitering as "wandering about from place to place without apparent business, such that the person poses a threat to public safety." Under these laws, if a police officer sees someone loitering, the officer can demand identification and an explanation of the person's activities. If the person fails to comply, the officer can arrest the person for loitering. Therefore, the refusal to answer questions is a problem only if the officer has also observed the person loitering.
Many people argue that police officers use loitering laws to clear neighborhoods of "undesirables." Some courts have held loitering laws to be unconstitutional on the grounds that they are enforced discriminatorily against poor people and members of ethnic minority groups, and that they unduly restrict people's rights to travel on public streets. The safest place to challenge the validity of a loitering law is in the courts, not on the streets to a police officer's face.
For a full explanation of the law in your state—and how it applies to your situation—consult a qualified criminal defense attorney.