First, I think it's important to consider that J.K. Rowling has inconsistencies throughout her series that sometimes simply cannot be explained; these are called "Flints", after the Slytherin student Marcus Flint, who accidentally appeared in two of the books as a seventh year twice in a row. J.K. Rowling later acknowledged that this was due to her own error, and it was not caught by her editors. There are numerous instances of seeming canon inconsistency in the Harry Potter series, just as an FYI, not as an insult or a slight toward the series. I'm pointing this out because I think there may not be a perfectly conclusive answer to this question. I interpret the answer as complex and layered. Canon examples follow.
Magic is an elemental, fundamental, and learned skill. There are numerous components involved in properly channeling it and "making it work". There are three types of spells in Potterverse (from jkrowling.com):
"Every now and then somebody asks me for the difference between a spell, a charm and a hex. Within the Potter world, the boundaries are flexible, and I imagine that wizards may have their own ideas. Hermione-ish, however, I've always had a working theory."
Spell: The generic term for a piece of magic.
Charm: Does not fundamentally alter the properties of the subject of the spell, but adds, or changes, properties. Turning a teacup into a rat would be a spell, whereas making a teacup dance would be a charm. The grey area comes with things like 'Stunning Spells', which on balance I think are Charms, but which I call spells for alliterative effect.
Hexes: Has a connotation of dark magic, as do jinxes, but of a minor sort. I see 'hex' as slightly worse. I usually use 'jinx' for spells whose effects are irritating but amusing.
Curses: Reserved for the worst kinds of dark magic. JK Rowling - jkrowling.com
Foremost, a child must be born with inherent magical ability; it cannot be learned by Muggles or Squibs, who lack the proper genetic abilities to produce magic. It is noted throughout the series that magical children have the ability to "make things happen', as Hagrid says, when they are scared, angry, or afraid (i.e. accidental magic). Examples of accidental magic include Harry's hair growing back overnight after Petunia Dursley cuts it to the scalp (SS/PS), Harry finding himself on top of his school cafeteria's roof while running from his cousin Dudley Dursley and Dudley's gang pre-Hogwarts (SS/PS), making the wrong-sized clothes fit (PP/SS), and blowing up his Aunt Marge like a balloon when angry (POA).
So, to cast spells at all, magical abilities must exist. It's clear from canon that different witches and wizards develop their magical skills at different paces, some being more prodigious than others. For example, at age eleven, we have Tom Riddle, who was previous unaware that what he possesses is called magic, but who is already channeling it without using a wand or incantations: In Half-Blood Prince, when Dumbledore meets Tom Riddle for the first time, Tom Riddle is excited to learn that he is indeed "special" (magical) and reveals to Dumbledore:
"I can make things move without touching them. I can make animals do what I want them to do without training them. I can make bad things happen to people who annoy me. I can make them hurt if I want to." (Tom Riddle -- Half-Blood Prince -- Chapter 13)
As well, Neville Longbottom laments that his family thought he was a Squib until he fell outside his house and bounced down the path, showing his first inklings of magic (SS/PS). In Chamber of Secrets, Hogwarts caretaker Argus Filch, who we learn is a Squib, trying to learn magic via Kwikspell mail-order study guides; he is unsuccessful in learning magic, as demonstrated in Half-Blood Prince, where Hermione notes that Filch is a poor wizard and cannot discern potions or poisons from regular concoctions, such as perfume or oak-matured meade.
Canon demonstrates that, at the very least, there must be a fundamental understanding of magic for spells to work. Yet, practice is also required -- technique matters. Pronunciation of the spell matters. As well, magic seems to react differently to individuals based on character and skill. Finally, the emotion a witch or wizard puts into casting a spell seems to affect the caster's ability to succeed. Note the following canon references:
PRACTICE AND TECHNIQUE
"Now, don't forget that nice wrist movement we've been practicing!" squeaked Professor Flitwick, perched on top of his pile of books as usual. "Swish and flick, remember, swish and flick. And saying the magic words properly is very important too -- never forget Wizard Baruffio, who said 's' instead of 'f' and found himself on the floor with a buffalo on his chest."
It was very difficult. (Sorcerer's Stone -- Chapter 10)
INDIVIDUAL SKILL OF THE WIZARD/WITCH
"The spell I am going to try and teach you is highly advanced magic, Harry -- well beyond Ordinary Wizarding Level. It is called the Patronus Charm [. . . .] But I must warn you, Harry, that the charm might be too advanced for you. Many qualified wizards have difficulty with it." (Remus Lupin - Prisoner of Azkaban -- Chapter 12)
"Harry, I can't believe it. . . . You conjured up a Patronus that drove away all those Dementors! That's very, very advanced magic. . . . " (Hermione Granger -- Prisoner of Azkaban -- Chapter 21)
"Impressive," said Madam Bones, staring down at [Harry], "a true Patronus at [fifteen years old] . . . very impressive indeed." (Amelia Bones -- Order of the Phoenix -- Chapter 8)
Regarding Slughorn's lunch on the Hogwarts Express in Half-Blood Prince, where Slughorn invites students he sees as special to eat with him, Harry asks Ginny Weasley:
"How come you ended up in there, Ginny?"
"He saw me hex Zacharias Smith [. . . .] When Slughorn came in, I thought I was going to get detention, but he just thought it was a really good hex and invited me to lunch. Mad, eh?" (Ginny Weasley -- Half-Blood Prince -- Chapter 7)
"Yes, those who progress to using magic without shouting incantations gain an element of surprise in their spell-casting. Not all wizards can do this, of course; it is a question of concentration and mind power which some" -- his gaze lingered maliciously upon Harry once more -- "lack." (Severus Snape -- Half-Blood Prince -- Chapter 9)
Numerous instances of Hermione Granger mastering magic through sheer determination, study, and practice are present throughout all seven books.
"Never used an Unforgivable Curse before, have you, boy?" [Bellatrix] yelled. She had abandoned her baby voice now. "You need to mean them, Potter! You need to really want to cause pain -- to enjoy it -- righteous anger won't hurt me for long -- I'll show you how it is done, shall I? I'll give you a lesson--" (Bellatrix Lestrange -- Order of the Phoenix -- Chapter 36)
"Avada Kedavra's a curse that needs a powerful bit of magic behind it -- you could all get your wands out now and point them at me and say the words, and I doubt I'd get so much as a nosebleed." (Mad-Eye Moody -- Goblet of Fire -- Chapter 14)
In Deathly Hallows, Harry casts the Imperius Curse, an Unforgivable Curse, on a Gringotts goblin and the Death Eater Travers as the trio tries to break into Gringotts to steal the Hufflepuff cup Horcrux, having never cast the spell before. He was under extreme duress in the situation, with Hermione as Bellatrix being suspected as an imposter upon trying to gain access to the Lestranges' vault. He was able to successfully cast Imperio even while using Draco Malfoy's wand. However, Harry does worry whether he cast Imperio "strongly enough". In total, Harry uses the Imperius Curse four times while in Gringotts.
"The Imperius Curse can be fought, and I'll be teaching you how, but it takes real strength of character, and not everyone's got it." (Mad-Eye Moody -- Goblet of Fire -- Chapter 14)
"First of all, Harry, I want to thank you," said Dumbledore, eyes twinkling again. "You must have shown me real loyalty down in the Chamber. Nothing but that could have called Fawkes to you." (Albus Dumbledore -- Chamber of Secrets -- Chapter 18)
"It is our choices, Harry, that show what we truly are, far more than our abilities." (Albus Dumbledore -- Chamber of Secrets -- Chapter 18)
Mr. Ollivander says in Deathly Hallows, "Oh yes, if you are any wizard at all you will be able to channel your magic through almost any instrument. The best results, however, must always come where there is the strongest affinity between wizard and wand. These connections are complex. An initial attraction, and then a mutual quest for experience, the wand learning from the wizard, the wizard from the wand." (Ollivander -- Deathly Hallows -- Chapter 24)
In Half-Blood Prince, Harry is able to cast Sectumsempra on Draco Malfoy without knowing what its exact effects would be; however, he was aware that Sectumsempra was "for enemies", and Harry carries strong negative emotions for Draco Malfoy throughout the series. Harry's emotions helped him to channel Sectumsempra properly against Malfoy, even though he wasn't fully aware of what Sectumsempra would do.
Also in Half-Blood Prince, Harry is seen casually experimenting with Levicorpus, which hoists Ron up into the air as if Ron's being held upside down by the ankle. Little effort seems to be required to perform this seemingly harmless jinx.
As J.K. Rowling has provided a spell tier, it would make sense that the severity of the spell, combined with the witch's or wizard's basic training, inherent magical abilities, character, and emotional state all come together to produce an act of magic. The logistics are complex and variable.