I omitted several sections in the simple example. There are sections to run commands before you install the package, after you install the package, after you uninstall the package, before you install the package, and before you build the package.
These sections are executed as bash scripts. You have access to the rpm variables as %{_variable} in most cases, you also have some other variables as bash variables $VARIABLE. Notably %{buildroot} is usually used as $RPM_BUILD_ROOT in these sections.
%pre
This is where you place anything you need to do before the package is installed on the system.
%post
This is where you place anything you need to do after installing the package. Typically you install users or start services in this section. If you installed libraries you would run ldconfig here. The kernel uses this section to setup grub and build initrd images for example.
%preun
pre-uninstall section is where you do things you need to do before you uninstall the package. You would stop a service here if you are uninstalling it. You would chkconfig --del the service to remove the symlinks in /etc/rc.d/runlevel. If your package uses alternatives, you would run alternatives --remove here.
%postun
post-uninstall is where you do things after the package is removed. If you added users in %pre, you remove them in %postun. The most typical thing to run here is ldconfig. If your package had installed desktop icons you would run update-desktop-database here and possibly gtk-update-icon-cache. You can't use anything that was in your package at this point, it's already gone.
%clean
This is an important section for builders. It is run before building the rpm. Typical use is to delete the working directory and install directory (%{buildroot}). If your package made some change during build that you wanted to make sure it did every time, you would undo the change here. For example, if your package requires an selinux boolean be set, you would unset it here to make sure your package is setting it correctly. Removing the buildroot directory is the only legitimate use I've found for this, you shouldn't really need to do more. You should be installing your packages in a sandbox after building them.