So the Pokeball, many people have theories, some good like the Pokemon has a living space, other lame ones how they get stored as light. My theory: when a Pokemon is captured, it becomes energy, but once fully in the ball, it becomes the Pokemon in a ball sized bigger than the mon/the pokemon become ball sized. Once in there, there is a layer where it is stored, and to break free, it must attack the wall before a timer ends. As the wall is attacked, 2 things can happen.
If the wall is broken by attacks, the Pokeball surface is revealed, and the Pokemon can break free. Obviously, other Pokeballs, and under certain conditions, have a much tougher wall to break. The Masterball, which has an extremely high rate may have an impenetrable wall.
If the wall is not broken in time, due to the Pokemon being tired, weak, or even wanting to stay in there, it is a successful capture.
After a successful capture, depending on the Pokemon's level, friendship with its Trainer, personality, IVs, and other factors like training, Pokemon type, and Pokeball type, the Pokeball will go from an empty void with a breakable wall, to a house-like environment. It can vary quite a lot, with infinite combinations. Let's say at a level 1, basic pokeball, no training, low friendship, bad IV, and mischief personality, Bulbasaur will have a small area of around 5 feet to roam and live, with minimal plants and a sad forest with only grass and a few plants, At Level 100, with good IVs, high friendship, and other good factors, the same Venusaur will have a much better place to be. What a small Bulbasaur had at the beginning, a 5 foot diameter sphere, would become an almost endless plain, lush with greenery, cave to live in, sunny spot to bask in, and other good things.
Now other Pokeballs also take place, like an Appletun. If 2 lvl 100 Appletun were both identical (same ivs, personality, etc), but one was in a Masterball while the other was in a Great Ball, things would be different. Greatppletun would have some sort of Apple farm to live in, roaming like there's no joyous end, but for Applemaster, it would have a whole mansion sized ranch where it has many more things that are noticable.
Finally, the last thing I have is about Pokemon being able to have free will. Most of the time, the Pokemon isn't doing much considering that it just has a home to live in. However, for certain Pokemon, like Misty's Psyduck, it comes out whenever it feels like. This could be because at the edge of the environment, there is the button used on the Pokeball, which it can use to break free if it wants. This can be fast travelled to. This button is always exposed, but on the first time, the button is blocked off by the wall and can only be pushed when the wall is destroyed.
Some common examples of certain mon houses are training gyms for fighing, ocean beach for water, wasteyard for poison, or anything that fits the pokemon.
We also have defective Pokeballs, like Ash;s Pikachu, which could be why it doesn't like the ball.