Use get_queried_object_id()
to get the ID regardless of what type of object it is- term, post, page.
There is also the get_queried_object()
function, which will give you more details, but will have some structural differences depending on object type.