How do I add classes to main menu ul and li in Drupal 8
Override the twig file for the main menu by saving menu--main.html.twig to your theme. Copy the original menu.html.twig from /core/modules/system/templates/menu.html.twig or /core/themes/classy/templates/navigation/menu.html.twig if your theme extends from Classy.

You can then modify the Twig macro used to generate the menu output. To get a class on the ul's to indicate level, you could do this:

{% if menu_level == 0 %}
  &lt;ul{{ attributes.addClass('menu', 'menu-level--' ~ menu_level) }}&gt;
{% else %}
  &lt;ul class="menu {{ 'menu-level--' ~ menu_level }}"&gt;
{% endif %}


You could do similar manipulations to the li's:

&lt;li{{ item.attributes }}&gt;

Just tack on .addClass('class-name') to attributes like so:

&lt;li{{ item.attributes.addClass('class-name') }}&gt;

If you're a HTML/class name snob like myself you can also remove classes like so:
{{ item.attributes.removeClass('menu-item') }}

Which I did on the line above the li in the for loop.