What language?? There are different tools for almost every imaginable programming language, since they all have different syntactic rules and conventions.
Good ol' indent is a nice, customizable, command-line utility to format C and C++ programs.
It depends of the language, and of the architecture you are using.
For example, in a php platform, you can format almost language with GeSHi
As bluish comments, GeSHi is a generic syntax highlighter, with no beautification feature. It is more used on the server side, and combine it with a beautification tool can be tricky, as illustrated with this GeSHi drupal ticket.