TL;DR: be consistent
Speaking from the other side of this: I once struggled severely with one math class in particular and ended up taking it 3 times. I failed the first two times (the first try was a dud - I was a terrible student; the second time I gave it a good try but found the subject very difficult) then the third time with my studiousness greatly improved and my understanding of the subject finally sinking in, I aced the class. Here's where it's most relevant to your question: I had the same teacher all three times.
As other answers noted, the students really ought to be upset with themselves. Even bad teachers - and I had some qualms about this teacher - are rarely the sole reason for failure, as if so many students were failing the school would or at least could be urged to intervene. A large part of my failure was my own habits and then my own difficulties with the subject. How did the teacher handle that, and how would I have preferred them to have handled it? Thankfully, both those questions get the same answer; that teacher did a great job in my opinion.
The teacher was respectful, fair, and aware. It was a relatively small class at a huge school, so ultimately a pretty large class (~60 students). She acknowledged knowing me with simple eye contact, smile, nod that sort of thing and knowing my name (which is unusual in such a big class) but she did not make me feel uncomfortably highlighted at all, she respected my space and left it up to me to stand out or hide. She continued holding me to the same standard as before the 2nd and 3rd time I took her class, not wavering to have higher or lower expectations, which I appreciate. Lastly, she didn't ignore or emphasize the situation, but she did acknowledge it where appropriate: as I came to her for help one-on-one she'd gently give advice on where she thinks I need to focus efforts on or just general study tips, encouraging me to come to more office hours and work with TAs if I'm struggling, but then she'd leave it at that. No dwelling on past failures, no extreme pushing to get me through (I mean, she did fail me twice...err, or rather I failed her course twice). Even as I started to succeed (I eventually aced the class!) she maintained the qualities I described above, albeit more encouraging and "keep up the good work" rather than "I recommend you _____________". Her consistency with me, and also between me and other students, was an important part of the respectful relationship we maintained through it all (even despite my grumpiness through some of it, especially in my bad-student days where I'd try to put the blame on her when it laid most solely with me in that first attempt).