[`the_ID()`](https://codex.wordpress.org/Function_Reference/the_ID) print the post ID. You need to use the [`get_the_ID()`](https://developer.wordpress.org/reference/functions/get_the_ID/) which return the post ID.

Example:

    foreach (get_the_terms(get_the_ID(), 'taxonomy') as $cat) {
       echo $cat->name;
    }

Always remember the naming convention of WordPress for template tags. `the` which mean to print `get` which mean to return in most of the cases.