Class active for Archive link – looped out

One way to do this is to get the current object ID with get_queried_object_id() and compare it against the category term ID inside the foreach loop. E.g.

$current_object_id = get_queried_object_id();

foreach (get_categories() as $category) {

  $classes="";
  if ( $category->term_id === $current_object_id ) {
    $classes .= 'selected';
  }

  printf(
    '<a class="%s" href="%s">%s</a>',
    $classes,
    get_category_link($category->term_id),
    $category->name
  );
}

is_category() should work too as it is basically a fancy wrapper for the same check as above.

foreach (get_categories() as $category) {

  $classes="";
  // Parameter can be category ID, name, slug, or array of such
  if ( is_category( $category->term_id ) ) {
    $classes .= 'selected';
  }

  printf(
    '<a class="%s" href="%s">%s</a>',
    $classes,
    get_category_link($category->term_id),
    $category->name
  );
}