The levels each slot offers
The table rolls a number from the bookshelves: 1 to 8, plus half the bookshelves, plus 0 up to the number of bookshelves. The top slot offers a third of it (at least 1), the middle two thirds plus 1, and the bottom the whole roll or twice the bookshelves, whichever is more. So with 15 bookshelves the bottom slot is always level 30, the middle 6 to 21 and the top 2 to 10. Each slot shows a level you need to have, but costs only 1, 2 or 3 levels and as many lapis lazuli.
From level to enchantments
The slot's level is then raised by the item's enchantability: twice a roll from 0 to a quarter of it, plus 1. That is why gold, with 22 for tools and 25 for armor, gets better enchantments than diamond at 10, and books, bows, crossbows, tridents and fishing rods at 1 get the least. Then the whole is moved up or down by up to 15%. Each enchantment the item can take at the table offers its highest level whose cost window holds that number, and one is drawn by weight: common ones like Sharpness, Protection and Efficiency weigh 10, rare ones like Silk Touch and Thorns weigh 1. After that, the table keeps adding enchantments with a chance of (level + 1) in 50, halving the level each time, and striking out any that clash with what it already drew. A book that comes out with two or more loses one at random.
Exact, not sampled
Every whole-number roll is counted, the 15% swing is worked out over its exact shape, and every order the extra enchantments can be drawn in is followed, so the figures are the true chances rather than the result of a simulation. Offers that clash with nothing and weigh the same are counted together, which keeps a book's 36 possible enchantments quick to work out. The figures follow Java Edition 26.3, whose enchantment weights, levels and costs come from the game's data. Bedrock Edition's table works differently, with its own level rolls and its own list of what each item can be offered, so these odds do not apply there.
What the table will not give
Treasure enchantments never come from the table: Mending, Frost Walker, Soul Speed, Swift Sneak, Wind Burst and the two curses. Some enchantments that fit an item on the anvil are left out at the table too: an axe is offered no Sharpness, Smite or Bane of Arthropods, a mace no Fire Aspect, Smite or Bane, and only a chestplate is offered Thorns. Use books and the anvil for those.
Questions
How many bookshelves do I need for level 30?
15. With 15 the bottom slot always offers level 30; with fewer it never does, since the bottom slot reaches 30 only from twice the bookshelves or a roll of 30, and a roll of 30 needs 15.
Is gold really better for enchanting?
Yes. Golden tools have an enchantability of 22 and golden armor 25, the highest of any material, so they get more and higher enchantments for the same level. They also wear out fastest.
Why do books get worse odds?
A book's enchantability is 1, the lowest there is, so its level gets almost no lift, and a book that draws two enchantments or more loses one. In return a book can carry any enchantment the table offers.
Can I reroll what the table offers?
Enchanting anything, even at the cheapest slot, changes what every slot offers next time. Taking bookshelves away changes the levels but not the hidden roll.
Does the hint on the slot tell me everything?
No. The hint names one enchantment from the list at random, and the rest stay hidden until you enchant.