The most practical way is to exploit etoolbox's utilities:
The problem arises for commands with "apparent arguments", for example
\section. If you look at the output of
\show\section, you'll see that it has no argument whatsoever. It's a technique frequently employed in LaTeX, because it's more efficient. Actually
\section is transformed into
\@startsection has six arguments; you see that
\section supplied the first five, the sixth is what comes after
\section (there's more to this, because there can be a * or an optional argument, but let's keep it simple).
Using the trick with
\appto for a command like
\section would result in a disaster, because the thing added will provide the sixth argument for
\@startsection, which is clearly not what is wanted.
As Herbert says, you can use
\appto also for environments, using
\endfoo if the environment is
foo, provided the environment has no argument. If the argument is part of the command (i.e. it is not "apparent") one can use
\apptocmd; if the argument is apparent (it's the case of
\tabular, for example)
\apptocmd must not be used, but
\pretocmd are quite safe.