There are two domains of language at play here: syntax on one hand (how subject and objects are ordered in the sentence), semantics and pragmatics on the other (what each part of the sentence means and where your focus is on).
In many "normal" Spanish sentences these two run nicely parallel: the order is subject, verb, object(s); the subject is normally the main topic of the sentence and therefore starts it; everything after the verb tends to be a commentary on the subject-topic. So the syntax (subject followed by verb and objects) aligns well with the pragmatics (topic followed by comment).
There's also something called the animacy hierarchy, that orders things according to their animate character (people come first, then animals, then plants and inert objects) and the like. This also tends to align with syntax, since it's often the case that subjects are people doing things while grammatical objects are nonhuman entities being done things.
These combined are probably the reason why the vast majority of the world's languages place the subject somewhere before the object(s) (in normal sentences).
What does this have to do with gustar and verbs like that? The thing with gustar and others like agradar, repugnar, apetecer, aburrir, hacer falta, etc. is that the standard syntactic order SVO goes against the animacy hierarchy and also inverts the topic-comment structure.
Take this "normalized" sentence (with the "proper" Spanish order SVO):
Las manzanas le gustan a Sonia.
This is not a semantically neutral sentence. If you wanted to say "Sonia likes apples" you wouldn't do it this way. The idea of "Sonia likes apples" is that you want to say something about Sonia, but the Spanish sentence, as it is, is a comment about apples! Spanish "wants" you to establish the topic first, and then commenting on it; and it "wants", if possible, the first argument of the verb to be more animate than the second. The latter is impossible using gustar; it would be possible if you used a different verb, like adorar:
Sonia adora las manzanas.
The only way to comply with the topic/animate - comment/inanimate structure, while keeping the sentence "neutral", is to move things around:
A Sonia le gustan las manzanas.
By placing Sonia first and then the apples, you have managed to produce a natural sentence that is equivalent to English "Sonia likes apples". You have also confused a lot of students of Spanish, native ones included, who expected the subject to come first.
Of course, Las manzanas le gustan a Sonia is also a correct sentence. But in this case you're changing the usual order for gustar and making the apples the topic, or starting theme, of your sentence.
A lot has been written about verbs like gustar, which feature "dative subjects" (things marked like indirect objects taking the place usually reserved for subjects). They're not exclusive of Spanish. They're basically nonexistent in English, but other related languages, like Icelandic and German, have them, as well as Russian, for example.
(Some other verbs of this kind, called by Melis et al. "emotional causatives", are: alegrar, atormentar, avergonzar, confortar, enamorar, enojar, escandalizar, espantar, maravillar, turbar, animar, asombrar, complacer, contentar, indignar, inquietar, molestar, ofender, satisfacer, admirar, regocijar, aburrir, asustar, fascinar, impresionar, interesar, irritar, preocupar, sorprender, aterrorizar, entusiasmar.)
For reference: Chantal Melis, Marcela Flores, Sergio Bogard. La historia del español. Propuesta de un tercer período evolutivo. (2003)