As the name implies, CBP (Customs and Border Protection) perform two roles:
- Checking that only people who should be allowed in are allowed to do so (that’s the “border protection” part, what in other countries would be called immigration or border police);
- Checking that no unauthorised goods are imported, or that any taxes and duties are paid, if relevant (that’s customs).
In some places like airports the two roles are apparently completely separate (passport control before baggage claim, customs after), but this is not really the case: if you meet an officer at passport control, they can already start evaluating you, and will pass a message to the customs line after baggage claim if they feel they may be something interesting.
At a land border the officer you’ll meet will likewise perform both functions: check paperwork (passports, visas…) and decide if you should be subjected to search.
So some of those questions are related to that: some screening to try to detect people who they think may have something of interest for customs. Customs apply to US citizens, PRs and visitors alike. If they want to unscrew every single bolt of your car before letting you go, there’s absolutely no “I am a US citizen you have to let me through” argument to be made. They’re not preventing you from entering the US, they are searching for contraband.
In addition to that, as others have written, they need to somehow verify that your documents are valid and that you are who you say you are. While a cursory check of your passport and matching your picture against the photo in there is what happens in most cases, sometimes they need to check a bit further. Also, when crossing a land border with a car or other vehicle, there’s always the possibility that there may be other people hidden in the vehicle.
Some questions are quite innocent, but they may reveal issues, often not so much in the answer (though if they ask for details which are written in your passport like your name or your date of birth and you get it wrong that could raise a few red flags), but rather in the way it is answered. Being nervous, trying to hurry things up, having inconsistent answers, all those sorts of things. Sometimes it’s a false positive and they press someone who really has no issues at all, sometimes it’s the start of interesting discoveries.
Border officers in the US, and this is true in many other places, actually have quite extensive powers, often much less limited or controlled than those of police officers.
If you are in a hurry, it’s definitely best not to be confrontational. They have all the time in the world, extensive powers, and they won’t let you go until they’re satisfied. Best to keep them happy.
Of course, there are actually limits to their powers (though some of those are subject to debate, I believe). Civil rights organisations have quite a few ongoing battles with them. If you have the time and energy you are welcome to make sure they obey laws and constitution. If you are in a hurry, better to let them do their thing.