get_the_category()
will retrieve the category of current loop item. In your case, it’s probably the last post of your main archive query.
To get the category of currently displayed category in the archive page, you can use get_queried_object_id()
:
// Check if this is a category archive
if ( is_category() ) {
// Get the current category ID
$category_id = get_queried_object_id();
}