Can CBP deny entry even with a valid visa?
Yes. A visa only lets you ask to be admitted, and a CBP officer decides at the port whether you are admissible and for how long.
Yes. A visa is permission to travel to a port of entry and ask to be admitted, not a promise of admission. The State Department says so on its own pages: the visa does not guarantee entry, and a Customs and Border Protection officer decides at the port whether you are admissible (Travel.state.gov, visa types). Most visa holders are admitted without difficulty, but the officer has legal authority to refuse.
Why an officer refuses
The grounds are the same ones a consular officer applies, found in the inadmissibility list in the Immigration and Nationality Act. In practice the common ones are a purpose that does not match the visa (a tourist who says they are going to work), doubt that the visitor will leave, a fact that came up since the visa was issued, such as an arrest, and missing documents that the visa category requires, such as a student's Form I-20.
An officer can also find that the traveler gave a different account of the trip from the one given at the visa interview. Small differences are normal. A difference that goes to the heart of the purpose is a reason to send someone to secondary inspection.
What happens when entry is refused
The officer has choices. The traveler may be permitted to withdraw the application for admission and take the next flight home, which leaves no formal removal order. Or the officer can start expedited removal under INA 235(b)(1), which carries a five-year bar on returning (8 CFR 235.3). A person who is offered withdrawal is making a choice with consequences, and an officer's decision to order removal instead depends on the facts, most often on a finding of fraud or misrepresentation.
A visa may be cancelled, with a stamp across it, when a person is refused entry because of a ground that also makes the visa invalid. That is not automatic, and it does not follow from every refusal.
What lowers the risk
Carry what supports the stated purpose: the invitation, the I-20 or I-797, proof of enrolment or employment, the address, the return ticket and enough evidence of funds. Answer the questions asked, in plain terms and consistently with the visa application. If you do not understand a question, say so.
A traveler may ask for an interpreter. A traveler cannot require a lawyer at the primary or secondary booth in most cases, but may ask to contact one, and a green card holder or citizen has additional rights.
Who this applies to
Every nonimmigrant visa holder, including students, workers and visitors. It applies to Visa Waiver travelers too, with fewer options for review. It does not apply in the same way to US citizens, who cannot be refused entry, or to returning permanent residents, who have a hearing right.
Anyone who has been refused admission or asked to withdraw may want to speak to an immigration attorney before applying for a visa again, since the question will be on the next form.