If a purpose is unlawful, or a lawful purpose is to be carried out by unlawful means, it is not charitable. For example, a school for thieves might, in a sense, advance education, but it is not a charitable institution.
However, this is not to say that any breach of the law would cause a charity to lose its charitable status. In the course of carrying out their charitable purposes, actions may occur that are not in accordance with the law.
This webpage gives examples of situations where unlawful and other actions occur, but each organisation’s charitable status is maintained. It is not an exhaustive list. There will also be many other instances.