How do the extension attributes work in Magento 2?

Extension attribute is a way to extend the Interface. Let's take as an example the first link you provided to the ProductAttributeMediaGalleryEntryInterface. If you look at methods there you'll see that it has this method

/**
 * Retrieve existing extension attributes object or create a new one.
 *
 * @return \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionInterface|null
 */
public function getExtensionAttributes();

Note the @return type of the method ProductAttributeMediaGalleryEntryExtensionInterface -- this is an interface which would be re-generated if you define extension attribute for ProductAttributeMediaGalleryEntryInterface (by default it is generated empty with no methods). The name of the attribute you registered will be used to create methods of interface.

Let's assume you added attr1 of type string. What you can do after interface get regenerated is to access it from the instance of interface.

$entity = $objectManager->get('..\ProductAttributeMediaGalleryEntryInterface')
$entity->getExtensionAttributes()->getAttr1();

To set the attribute, you would need to instantiate Extension Attributes interface

$extension = $objectManager->get('..\ProductAttributeMediaGalleryEntryExtensionInterface')
$extension->setAttr1('value');
$entity->setExtensionAttributes($extension)

The latter is default scenario available, it might be simplified depending on how ExtensionInterface and parent interface are implemented.

[Updated]

Custom attributes and extension attributes serve different purposes.

Custom attributes are needed to represent the EAV attributes of the entity. Most of the EAV attributes are dynamic: they can be added after Magento is deployed via the admin UI. That's why you cannot get code auto-completion for EAV attributes: you don't know about all of them ahead of time.

However, as extension developer, you do know about some attributes for sure -- the ones which you created at development time. It can be a new field in database, field in the related database or an EAV attribute. You can register them as extension attribute because they never change unless the codebase is changed. You can get code auto-completion for them.