Depending on your Linux distro and release, ultimately sysvinit scripts (those scripts in /etc/init.d/) are generally run from the symlinks which exist in /etc/rc[0-6S].d, by /etc/init.d/rc.
Under Ubuntu, you're either using old-style sysvinit, or more recently, upstart. Under the upstart management directory (/etc/events.d/) you'll find a legacy mode that falls back to the /etc/init.d/rc invocation. Otherwise, /etc/init.d/rc is invoked for each runlevel via /etc/inittab.
If you examine the logic of /etc/init.d/rc, you'll find it defines actions depending on runlevel (0 & S are unconditionally stopped) or script prefix (S[0-9][0-9]* scripts start, K[0-9][0-9]* (kill) scripts stop). The numbering of scripts within a runlevel directory (e.g.: /etc/rc1.d/) determines the order in which scripts are stopped or started. Kill scripts are run first, then start scripts.
For more on this, research sysvinit and upstart.
man init). Maybe you'll get some insight by looking at it's source code.