This article shows how you can display list of post categories in wordpress. Wordpress has a in-built function called wp_list_categories($args). Using this function you can display all the categories along with sub-categories also. You just have to pass the appropriate arguments to the function.
<ul> <?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0'); ?> </ul>
Above code snippet will only display the categories that are assigned to any of the posts. You can find the list of arguments on following wordpress codex.