There's no foolproof way to keep your vassals from rebelling. In fact, it can be to your benefit to have them try, then beat them. What you can do is keep their loyalty high, and make sure that you're able to put down any rebellions that do occur.
Short answer: have high prestige and a good reputation, give land to your children, bribe, lower scutage, pass feudal contract and elective law. Long answer:
There are a lot of little tricks to this, but I'll focus on a few points that aren't intuitive. One is that, when you mouseover the loyalty score on a character sheet, you'll get an explanation of how it changes each month and why. You should make a habit of checking this until you have a good idea of what affects loyalty.
You should also try to give land to people who will stay loyal to you, such as your friends and male-line relatives. Within the game, your wife, your daughter's children, and even your mother, do not count as part of your family. It's especially important to give your heir titles, since that will mean he starts his rule with high prestige. As all vassals start out less loyal to a new king, he'll need it. It can also help your reputation, prestige and efficiency to give land to people whom you or your son will inherit. Be aware that your daughters will not usually marry without your blessing, and the "Daughter is unmarried" penalty to prestige really means, "Daughter is unmarried and living at home," so giving your unmarried daughter land is a good way to provide for her, get a loyal vassal and keep the land in the family. If you want to marry her off instead, and get the "marriage of eldest daughter duty," marrying her to a vassal both gives her a wealthy, titled husband and raises your son-in-law's loyalty.
Handling peace terms properly is crucial to pulling out of a death spiral, and the way the game treats them surprised me, but actually is very simple. The prestige you gain equals your war score minus your peace score. The result of this is that, when a vassal rebels, you don't want to make an example of him. What will really impress everyone is if you conquer his entire demesne, then recognize all of his titles and let him keep all of his claims on yours. The reason for this is that a revolter almost always claims your primary title. The game thinks that a claim on a king title is worth a lot of peace score, so that a king won't easily give it up to a pretender, but what this also means is that it's worth a lot of peace score to make a defeated pretender renounce his claim. Because each point of peace score you don't use goes directly to your prestige, letting him keep his claim gets you thousands, or even hundreds of thousands, of prestige points. Little details such as whether he becomes your vassal or keeps his land and money are trivial in comparison. This can easily make the difference between losing prestige from winning a war, and becoming the most respected sovereign in the world.
Your reputation will worsen if you take and keep a duke title, but you can take one and give it to a more loyal count without a problem. You'll also damage your reputation by taking count titles, and giving them to other people will only mitigate the effect. You can most quickly repair a bad reputation by giving away your land; if your father created heirless duchesses who'll leave your land to you, or an archbishop who becomes pope, your family can even give away the same land over and over. Piety has a second-order effect here: it improves reputation, which improves vassal loyalty.
Feudal contract law will make your vassals happier, and royal prerogatory law will upset them (even after they've accepted it). Elective law will make them happy, too, for a different reason: your vassals will become your heirs. You also have more flexibility to manipulate the succession so that the most talented member of your dynasty inherits. One disadvantage is that you'll have to work a lot harder to manage the succession, and another is that your heirless duchesses will no longer pass on their lands to the king, but to the formerly-ruling branch of the family.
One last piece of advice: always keep a larger, richer demesne than any of your vassals, and call up your vassals' vassals' armies if you need more troops; lowering their loyalty won't hurt you. As this isn't a tactical wargame, the larger army should win.