Assign specific ads to specific categories/posts

You have attempted to paste your PHP into a text widget. and the tags have been transformed.

<div class="textwidget">          <!--?php if (is_category('5')) { ?-->

You can’t execute PHP from a text widget by default, and I would not recommend trying to do so (it should be possible). Instead, it is trivial to build your own widget to do this.

Based on that other answer:

class PHP_Widget_wpse_135949 extends WP_Widget {
  function __construct() {
    $opts = array(
      'description' => 'Display Some ads'
    );
    parent::WP_Widget(
      'my-ad-content',
      'Some PHP',
      $opts
    );
  }
  function widget($args,$instance) {
    // Your PHP goes here
    // 
    echo 'PHP generated content';
  }
}
function register_my_widgets() {
  register_widget('PHP_Widget_wpse_135949');
}
add_action('widgets_init','register_my_widgets');