College is hard. Some classes are really hard. As much as we'd all like, our grades don't always go the way we plan. Going into my spring semester of sophomore year, I had never received a finalized grade lower than a single B+ in biology. Now, I'm sitting with a handful of B's and planning to retake a class I didn't even pass. It's hard to swallow failure when you aren't used to it, and it's even harder to swallow when you knew you did everything you could to succeed. But while it does suck, it's not the end of the world. Now isn't the time to give up; it's the time to see what you need to work past to get to where you want to be.
First thing's first: damage control. If your semester isn't quite over yet, do anything and everything you can to try to raise your grade in the class. Do all the homework, and don't skip any lectures. Going to office hours can help too, as many professors will be more lenient with the grades of students they know are actually trying. While all of that is good, the main thing you should be doing is studying. Studying is awful--there's a reason the word "dying" is in it--but it really does help. Don't just review everything for the sake of itself. Study the things you know you don't know, and be open to different ways of studying, like making study guides and flashcards or reviewing with classmates.
If your semester has already ended and grades are finalized, there isn't as much wiggle room to improve, but have hope. Many schools have grade replacement policies, so check to see what can be done to help your GPA in the long run. Some schools, such as the University of Kansas, offer grade replacement for some freshman-sophomore level classes. That means that if you retake the class and do better in it, the new and improved grade will replace your old one. Not every class will have an easy fix though, but you can almost always retake a class. By retaking a class, you can average the two scores together in your GPA and lessen the damage of the first grade.
Whatever your situation is, it is important to realize that a bad grade isn't always completely on you. Mental health, poor teacher instruction, and other outside factors can play a role in getting a bad grade. Analyze your own situation to figure out where the problem came from, and fix what you can. If you're partying too much, try easing back a little. If you're procrastinating, find ways to help yourself be more proactive. If the problem is out of your own control, find someone on campus to talk to for help. But overall, don't be too hard on yourself. Mistakes happen. No one is perfect, and no one should expect you to be. Give yourself time to process what happened, and then move on. Failing is an awful feeling, but you can use it to your advantage if you choose to learn from it instead of letting it break you. Don't let a few stumbles along the way prevent you from being where you want to be. Swallowing a failure does hurt, but don't let yourself give up.
Information taken from KU Undergraduate Advising Center, "Credit/No Credit, Withdrawal, Repeat," and Thought Co., "What to Do if You Fail a Class in College."