The other answer is correct that some cats bite to get your attention, but that's not the full story.
There are also cases where a cat bites you because it wants to give you affection, and that's how they do it.
This is the case for one of our cats. She's relatively shy, and only comes for pets once in a blue moon. When she does, she really enjoys it, that's clear to read from her behavior.
She also starts biting after you've been petting for a while. Initially, we thought that we were petting somewhere she didn't like; but observation quickly showed that it wasn't related to the location of the petting.
The only correlation was that the biting would be more frequent (and a bit harder) when she was enjoying it more (louder purring, more forcefully pushing herself against my hand, more arching of the back)
We've seen this behavior for over a year now, and it's always been the same. We've also observed her doing the same to her sister when she (the sister) washes her (the biter).
It's important to distinguish whether the cat is biting you to get affection, or to give affection, because you should respond differently to these.
Getting affection
This sets a bad precedent. If you give in to their demands, you're reinforcing the biting behavior.
You can start by ignoring it, or (softly) pushing the cat away from you when it bites.
I'm a bit more proactive about showing my dislike for their behavior. If they misbehave on the bed; they no longer get to be on the bed. I give them a stern "No", maybe a second chance, but then they get pushed off the bed and will not get attention.
It may take a few tries for your cat to understand that the bites are what causes your unhappy response, but they'll see the emerging pattern if you behave consistently.
Giving affection
This is relatively harmless behavior, as long as they don't bite hard enough to actually hurt or wound you.
When our cat started off with soft bites, barely noticeable, I didn't tell her off for it. It's her way of showing affection, and telling her off would come across to her as if I were telling her off for being nice to me. I didn't want her to think that I didn't like getting her affection.
However, there is a line of what I find acceptable. She has crossed that line (unintentionally) a few times, biting harder than I'd like her to.
Whenever she crosses that line, I act as if she really hurt me. I yell "ow", pull away my hand (or whatever part of me she bit), and will rub my hand on it while looking at her with a questioning/upset look. After 10-15 seconds, I'd revert to petting her.
If it happened a second time in a row, maybe a third (depends on my patience), I would yell out louder, pretend to tend to my wound, and then no longer interact with her.
Don't punish the cat for biting too hard. It's most likely that they weren't aware that they were hurting you (humans are relatively softskinned compared to cats). Telling her off will make the cat feel like you don't want to get affection from them, which damages your relationship.
The goal here is to pretend that you're hurt. You should shy away and be afraid of the cat, not get in its face and tell it off for biting.
Think of what your cat would do if you stepped on its tail. It would cry out, distance itself from you, and observe you to see if you're actively trying to hurt them more. If it's a one time mistake, the cat should see that you mean them no harm and should let go of it after a while. If you do this repeatedly, the cat will distance itself more and more.
You're trying to behave the same way. Your cat hurt you, but you don't understand why. You distance yourself and look at it, worried if it's going to hurt you again or not. When the cat behaves nicely, you give it another chance. But repeated bites cause you to distance yourself more and more.
Cats understand your behavior, because you're behaving like a cat. That's why they eventually understand the meaning of your behavior.
It took our cat about 10 times to register that she was hurting me, but the message did get across. Now, after she bites me too hard, she sometimes comes and licks the spot that she bit, trying to make amends.
Other times, even if I only slightly jerk my hand/arm away, she immediately gets the hint and stops biting.
Consistent behavior is key here. The cat needs to either consistently be told off (if it's about getting affection) or reprimanded when they cross a line (if it's about giving affection). If you are consistent about that line that they must not cross, they will eventually learn to not cross that line anymore.
Update
Another thing to consider here, is that cats will learn to steer their behavior based on the consequences of their actions. The same cat, when she was younger, was very playful. She would pounce on our feet for the slightest movement we made.
This was a problem that needed to stop. But it sort of fixed itself.
Whenever she pounced on my feet, I would instinctively flick my foot to get her off. About half of the time, I would inadvertently end up smacking her in the face with my foot.
I never really hurt her (it wasn't that hard), but she consistently experienced the same outcome: pounce on the foot, likely to get smacked in the face.
Once or twice, she pounced really hard, and ended up shoving her with my foot (my SO saw me do it but I wasn't awake).
After about two weeks in total, she stopped pouncing on our feet. She had learned the hard way that it came with drawbacks.
The same can be true of your case. If your cat is up in your face while you sleep, and your consistent reflex is to swat at things that are near your face; then they will eventually stop doing it because they don't like getting swatted.
This is really a case of deciding where you want to draw the line, and then sticking to it.
- If you don't want them on the bed when you sleep; then never be welcoming to them being on the bed while you sleep.
- If you don't want to be bullied into giving them affection, then ensure that their demand for affection ends up with a negative outcome for them (pushing them off the bed, negative response, rolling over)
- If you don't mind their presence but don't want to get soft bites, then behave apprehensively whenever they soft bite you. If they repeat the behavior, react more strongly to it.
- If you don't mind any of it, don't punish them for any of it.