it is supposed to do both, prevent bugs by the fact that you capture behavior in test and improving design by leveraging natural laziness of humans to take less effort to achieve results. Usually less effort approach results in clean better structured code at least that is what I observed
First write the test. This is supposed to make you think more about the API and how you'd like to use it. Then write the implementation now that you've effectively tried using the API by writing the test. That why it's called Test DRIVEN Development. If all you're doing is writing unit tests that's not really TDD, that's just writing tests.