Throughout the various editions of the game, the cosmology of the D&D settings has changed here and there. For example, the cosmology for the Forgotten Realms setting got completely rebooted in the 3rd edition of the game, and was rather different from the one that you will read below. It was a much more simplified cosmology with the Outer Planes likened to a metaphysical tree whose branches mostly comprised the realms of the deities.
However, since the original tag for the question is "lore", I will describe the "Great Wheel Cosmology". This cosmology is the one referenced most often throughout the history of the game going as far back as 1987, when the original Manual of the Planes was published for AD&D 1e. The 5th edition material published so far also describes most of the components below, with the exception of crystal spheres and phlogiston (though their existence in 5e is implied by Mike Mearls in this tweet).
World: Forgotten Realms setting takes place on a continent called Faerûn on the planet Toril. There are many kingdoms, city-states, etc. on this continent, such as Corymr, Sembia, Amn. Toril is the third planet on a star system.
Crystal Sphere: Toril's star system is located within a "crystal sphere" called Realmspace. Much like how various ancient cultures envisioned it, the stars are actually points of light (possibly gates to the elemental plane of fire) that are embedded inside a solid spherical shell that surrounds the star system.
Phlogiston: Crystal spheres are embedded in some material called phlogiston. Travel through the phlogiston is possible through spelljamming ships. Other D&D worlds, such as Oerth (planet for the Greyhawk setting) or Krynn (planet for the Dragonlance setting) are embedded in their own crystal spheres (Greyspace, Krynnspace, etc.).
Prime Material Plane: All the crystal spheres together make up the prime material plane. For an average commoner, her own crystal sphere is her universe (for example, even the deities she worships can be single-sphere powers); while an informed sage could consider the entire prime material plane as "the universe". Everything in the prime material can be considered to be composed of two kinds of essential components: matter and soul/belief.
Inner planes: Fire, earth, water and air are the four basic elements (fundamental building blocks) of matter. Along with the positive (life) and negative (undeath) energies, these elements are the substance of everything in the material universe. Each of these elements and energies can be found abundantly in "pure" forms in so-called inner planes. These are essentially locations outside of our "normal universe" with an infinite supply of their respective stuff. Inner planes are connected to the prime material plane through a conduit plane called the Ethereal plane.
Extradimensional spaces: These are pockets of space (empty or filled with matter) of various sizes that are embedded in the Ethereal Plane. For a magical item that has a much larger inside volume than seen from the outside, like a bag of holding, the extra volume is quite often such an extradimensional space.
Dimension: If you likened the prime material plane to a 2-dimensional sheet of paper, the Ethereal Plane would be like a block resting on that sheet, with one face touching the prime's every location. Within that block, the regions that are closest to the prime are called the 'border ethereal'. Quite often the features of the border ethereal resemble the locations in the prime material that they are in contact with; it is even possible to have entire realms extending deeper into the ethereal, looking just like distorted/modified reflections of the prime. Examples of such "dimensions", are the Feywild (Plane of Faerie) and Shadowfell (Plane of Shadow). In earlier versions of D&D, all these locations would generally be called 'demiplanes', in 5e the word demiplane seems to refer to somewhat smaller domains in the Ethereal Plane. (For example, the Demiplane of Dread, ie. the location for the setting Ravenloft, is accessible from the remote reaches of Shadowfell.)
Outer planes: All of the "belief", stuff that we generally associate with whatever lies beyond matter, such as good/evil, chaos/law, and many of the realms of the deities, etc., get some sort of concrete existence as the 17 outer planes. There is one outer plane for each alignment (LG to CE), as well as one plane between any non-neutral alignments (for example between LG and NG). Various planar beings that the embodiments of their respective alignments (like demons and angels, etc.), as well as the petitioners ('souls' that depart the prime material plane when people die), can be found in these outer planes. When you cast a Raise Dead spell, you are effectively bringing a petitioner back into her prime material body. Outer planes are connected to the prime material plane through a medium called the astral plane.
Multiverse: All the above listed planes of existence make up the multiverse. Except for the prime material plane, all the other planes have their own rules for (meta)physics. Travel between the planes is possible only through magic (magic portals, gates, or powerful spells like Planeshift).
PS: You can watch Mike Mearls describe some of the topics described above in a video here on D&D Beyond. (Thanks to @DavidCoffron for the pointer.) Further details can be found in the second chapter of the Dungeon Master's Guide.
Regarding the definition of realm, it is used as a generic word in many different contexts, describing a dominion or a sphere of influence or a country. For instance we can talk about the elven realms in Evermeet, deity Mystra's realm in the outer plane of Elysium, etc. You can even hear people refer to entire planes or crystal spheres as realms. Some of Toril's inhabitants call their world "The Realms", while we additionally use the adjective "forgotten" in the name of the setting to indicate that we (those of us living on Earth) used to have regular contacts with Toril in the past, but mostly forgot about their existence later.
Far Realm: Introduced in The Gates of Firestorm Peak, a 2e module published in 1996, the Far Realm is another multiverse, completely alien to our minds. Some creatures of type 'abberration' such as aboleths and beholders are said to originate from or get influenced by the Far Realm.
Best Answer
D&D 5e relies a lot on older material from previous editions, particularly where cosmology is concerned. It makes explicit reference to the “multiverse,” and there are myriad suggestions that things are not supposed to be different (for most things) in 5e than they were before, but the details ultimately aren’t found in 5e and it will always be at least a little speculative how much still applies, since we know at least a few things are explicitly different (for example, 5e has the Weave be found throughout the multiverse, which it never was before and which conflicts fairly heavily with details of magic in other settings).
But still, the best source of details here are the Spelljammer and Planescape settings from AD&D 2e (and some 3e/3.5e for Planescape).
Spelljammer deals with the Prime Material Plane, which is subdivided into Crystal Spheres, which are effective solar systems encased in a giant, well, crystal sphere. Each crystal sphere has one or more stars, one or more planets, and on at least one of those planets, a campaign setting we’re familiar with. For example, the continent of Faerûn in which most of the Forgotten Realms takes place is on the planet Toril, which is found in a Crystal Sphere known as Realmspace. There is a separate Crystal Sphere called Greyspace, in which you’d find the planet Oerth, where the Greyhawk campaign setting is found. The Crystal Spheres float in a substance known as Phlogiston, and currents—known as Flows—sweep past them in predictable layouts that can be charted and so on. Spelljammers fly through the Flows to visit separate Crystal Spheres. This is the basic premise of the Spelljammer setting.
The important thing about Crystal Spheres is that they are governed by mysterious beings known as overdeities. For example, Ao of the Realmspace. For my money, it’s easiest to just say that overdeities aren’t deities at all, just something “over” them—because most of the rules of deities don’t apply to overdeities.1 Anyway, the point is, overdeities set the rules for their Crystal Sphere, including stuff like how magic and faith work, how gods are allowed to operate, and so on. Ao is known to have set quite a number of stringent rules in Realmspace, for example, and is also known to update those rules as he sees fit.
Separately from the Crystal Spheres—which are all found on one plane, the Prime Material Plane—we have other planes. Inner Planes of elements and energy, Outer Planes of alignment and belief, and Transitive Planes between them (Astral, Ethereal, Shadow, maybe Feywild). All Crystal Spheres are connected to the same set of other planes, and beyond the Crystal Spheres, overdeities have no say. So while Shar has to play by Ao’s rules in Realmspace, she’s under far fewer obligations in, say, the Outer Plane of Hades, which is where 2e put her divine realm, the Palace of Loss.2
And it’s the rules on the Crystal Spheres which explain the differing divine populations of each Sphere: not everyone wants to play by Ao’s rules, for example. Being worshiped on many Spheres is a good thing for a god, but there are a lot of Spheres out there and it’s not always worth it to try to expand to one particular Sphere, especially if the overdeity’s rules are against you. Moreover, expanding to a new Sphere is hard—even if one of your faithful gets there and manages to attract a flock, local deities can and will try to masquerade as you and receive that worship in your stead. (Shar is particularly infamous for this, which she has done successfully even against local deities.) Especially in Spheres with onerous rules—like Realmspace—local deities who are already established can have a nigh-insurmountable advantage. For instance, Shar is worshiped nowhere else, but in Realmspace she’s a huge deal—outside the Realms, plenty of other deities could tell her to sit down and shut up, but in the Realms she can tear nearly anyone else apart. Between Ao’s rules and the presence of powerfully-established local deities, Realmspace does not look like a good candidate for expansion.
The reverse is also true: Shar isn’t likely to be interested in starting from the bottom of the totem pole in some other Sphere. Dealing with Realmspace and her rivals there already occupies a lot of her attention, and expanding would be painful and possibly humiliating for her. So why should she? In theory, if she expanded a lot, it would substantially increase her power outside the Realms, but the Realms are what she cares about and while it would help there too, she’s already about as powerful as anyone can get there—there just isn’t a whole lot of growth potential for her that way. Better to try to tear down the few Realmspace rivals that are on her level (most notably Selûne). Expanding would be difficult and risky, for only marginal benefit.
The end result is that most deities avoid Realmspace (because the rules are difficult), and the deities who do interact with it mostly specialize heavily on it (because dealing with the rules often requires focus), so you get a mostly-unique population of deities in Realmspace. Other Spheres have easier rules, and so it’s easier for deities to be there as well as other Spheres, and you see more overlap. Greyspace is a lot like this, which is why you see gods there that are also found in many other worlds.
5e has—somewhat confusingly, both because the label seems a misnomer and because the label was used with a different meaning in previous editions—labeled these “greater deities.”
3e put the Palace of Loss in the Plane of Shadow, and 4e did something weird with the Towers of Shadow embedded in the Astral Plane—it’s usually best to ignore 4e for these discussions though, as its handling of canon was widely panned and largely reverted or ret-conned in 5e.