How Magic Actually Works in Harry Potter
The spoken rules
The Harry Potter magic system is deliberately loose, which is part of its charm. Unlike systems in harder fantasy, where every spell has a cost and a limitation, magic here runs mostly on intent, skill and emotional state.
But there are rules. Dumbledore names five exceptions to what magic can do: it cannot raise the dead, it cannot create true love, it cannot create food from nothing (though it can multiply and transform food that already exists), and the final two exceptions are implied rather than stated.
The most important rule is one that drives the entire plot: magic cannot undo death. The Resurrection Stone doesn't truly bring people back; it summons shades. The ghosts at Hogwarts aren't fully alive. Voldemort's Horcruxes don't cheat death so much as tear the soul apart to avoid it. Every attempt to get around this rule ends badly.
The unspoken rules
Certain patterns emerge from how magic is described across the series. Nonverbal spells are harder than spoken ones, which is why students don't learn them until their sixth year. Wandless magic is even harder and seems to require unusual power or extreme emotion. Accidental magic in children is driven entirely by feeling, which is why it happens during moments of fear or anger.
The Unforgivable Curses require genuine malice to work. Harry tries to use the Cruciatus Curse on Bellatrix Lestrange after Sirius's death and fails, because his rage isn't sustained enough. This implies that evil magic is harder than good magic, or at least that it requires a different kind of commitment.
The Patronus Charm is the clearest example of magic that depends on the caster's inner state. It requires a powerful happy memory, and it takes the form of an animal that reflects the caster's deepest self. Snape's Patronus, a doe, matching Lily's, is the detail that reveals his entire story.