In Drupal 7, there was a global functions for adding headers to the HTTP response: drupal_add_http_header()
Developers could use a special-case header called 'Status' to change the response code.
In Drupal 8, this function is gone. See: https://www.drupal.org/node/2160069
The ['#attached']['http_header']
portion of the render array is used to add headers and status code late in the render pipeline.
$render['#attached']['http_header'][] = ['Content-Type', 'x-application/custom-content-type'];
$render['#attached']['http_header'][] = ['Status', "418 I'm a teapot."];
These headers will be added at render time.
There are also two other ways to change the response code, depending on context:
1) Create an event subscriber for KernelEvents::RESPONSE
and change the status code there. See: \Drupal\Core\EventSubscriber\HtmlResponseSubscriber
for an example of this technique.
2) Throw the appropriate Symfony HttpException:
use Symfony\Component\HttpKernel\Exception\NotFoundException;
throw new NotFoundException();
See this change notice for an example: https://www.drupal.org/node/2017339
See also: drupal_process_attached() has been deprecated: https://www.drupal.org/node/2565285