What's in an (accessible) name?

Most UI has good accessible names by default. But when do you need to write your own? And how?