I used this code:
$categories = wp_get_post_categories(get_the_ID());
foreach($categories as $category){
echo \'
In the code you gave us you are selected the categories selected for the specific post get_the_ID() is doing that part. However you would be best off using another function get_categories() https://developer.wordpress.org/reference/functions/get_categories/ which you would do like so:
$categories = get_categories();
foreach($categories as $category) {
echo '';
}
You can also pass through arguments to be more specific (if needed) - see https://developer.wordpress.org/reference/functions/get_terms/ for details on what you can pass through