Perform a query with an entity field condition with multiple values
Use two separate andConditionGroup():

$query = \Drupal::entityQuery('node')
  -&gt;condition('status', NODE_PUBLISHED)
  -&gt;condition('type', 'custom_type');
$and = $query-&gt;andConditionGroup();
$and-&gt;condition('custom_taxonomy', 2);
$query-&gt;condition($and);
$and = $query-&gt;andConditionGroup();
$and-&gt;condition('custom_taxonomy', 8);
$query-&gt;condition($and);
$result = $query-&gt;execute();


This works no matter how many terms are in the field or in which delta they are.

Edit

This results in this SQL:

SELECT base_table.vid AS vid, base_table.nid AS nid
FROM 
{node} base_table
INNER JOIN {node_field_data} node_field_data ON node_field_data.nid = base_table.nid
INNER JOIN {node__custom_taxonomy} node__custom_taxonomy ON node__custom_taxonomy.entity_id = base_table.nid
INNER JOIN {node__custom_taxonomy} node__custom_taxonomy_2 ON node__custom_taxonomy_2.entity_id = base_table.nid
WHERE  (node_field_data.status = '1') AND (node_field_data.type = 'custom_type') AND( (node__custom_taxonomy.custom_taxonomy_target_id = '2') )AND( (node__custom_taxonomy_2.custom_taxonomy_target_id = '8') )